Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable attachment of binary files as charm resources #1147

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions juju/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2215,16 +2215,24 @@ async def add_local_resources(self, application, entity_url, metadata, resources
"username": "",
"password": "",
}
data = yaml.dump(docker_image_details)
data = yaml.dump(docker_image_details).encode("utf-8")
else:
p = Path(path)
data = p.read_text() if p.exists() else ""
data = p.read_bytes() if p.exists() else b""

self._upload(data, path, application, name, resource_type, pending_id)

return resource_map

def _upload(self, data, path, app_name, res_name, res_type, pending_id):
def _upload(
self,
data: bytes,
path: str | Path,
app_name: str,
res_name: str,
res_type: str,
pending_id: str,
) -> None:
conn, headers, path_prefix = self.connection().https_connection()

query = f"?pendingid={pending_id}"
Expand All @@ -2235,8 +2243,8 @@ def _upload(self, data, path, app_name, res_name, res_type, pending_id):
disp = f'form-data; filename="{path}"'

headers["Content-Type"] = "application/octet-stream"
headers["Content-Length"] = len(data)
headers["Content-Sha384"] = hashlib.sha384(bytes(data, "utf-8")).hexdigest()
headers["Content-Length"] = str(len(data))
headers["Content-Sha384"] = hashlib.sha384(data).hexdigest()
headers["Content-Disposition"] = disp

conn.request("PUT", url, data, headers)
Expand Down
Loading