-
Notifications
You must be signed in to change notification settings - Fork 14
feat(instance): add support set and get user_data #827
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
Merged
Merged
Changes from 23 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
19360aa
add modul
Laure-di 57d42e8
rm module
Laure-di 621e4ba
feat(instance): add support set and get user_data
Laure-di fa118d5
set_server_user_data
Laure-di 9d71e24
fix import syntax
Laure-di c439aa7
fix request set
Laure-di 668bce7
clean code
Laure-di 3a176d7
fix to fit generation pattern
Laure-di 668f882
split in different file
Laure-di acb6856
update type from stringIO to bytes
Laure-di e70f5b4
fix change to bytes
Laure-di bbba017
clean code
Laure-di 2174262
fix format ruff
Laure-di e0ed8dc
Delete docs/module.rst
Laure-di aec4760
update python version workflow
Laure-di c2e264a
rm tests
Laure-di 09e9656
ruff
Laure-di 11194c8
fix linter
Laure-di ea4d093
fix linter scaleway-core
Laure-di a1da82c
fix linter scaleway-core
Laure-di 2c22542
add support get_all_server_data_user
Laure-di b75d23e
add set_all_user_data
Laure-di 7ad8012
fix ruff
Laure-di c923f91
add tests
Laure-di 0a363eb
fix ruff
Laure-di 0564013
skip test
Laure-di 3d3a11c
Merge branch 'main' into instance-user-data
Laure-di a5b5d50
Merge branch 'main' into instance-user-data
Laure-di File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| from typing import Optional, Dict | ||
|
|
||
| from requests import Response | ||
|
|
||
| from scaleway_core.bridge import Zone as ScwZone | ||
| from scaleway_core.utils import validate_path_param | ||
| from .api import InstanceV1API | ||
| from .custom_marshalling import marshal_GetServerUserDataRequest | ||
| from .custom_types import GetServerUserDataRequest, GetAllServerUserDataResponse | ||
|
|
||
|
|
||
| class InstanceUtilsV1API(InstanceV1API): | ||
| """ | ||
| This API extends InstanceV1API by adding utility methods for managing Instance resources, | ||
| such as getting and setting server user data, while inheriting all methods of InstanceV1API. | ||
| """ | ||
|
|
||
| def get_server_user_data( | ||
| self, server_id: str, key: str, zone: Optional[ScwZone] = None | ||
| ) -> Response: | ||
| """ | ||
| GetServerUserData gets the content of a user data on a server for the given key. | ||
| :param zone: Zone to target. If none is passed will use default zone from the config. | ||
| :param server_id: | ||
| :param key: | ||
| :return: A plain text response with data user information | ||
|
|
||
| Usage: | ||
| :: | ||
|
|
||
| result = api.get_server_user_data( | ||
| server_id="example", | ||
| key="example", | ||
| ) | ||
| """ | ||
| param_zone = validate_path_param("zone", zone or self.client.default_zone) | ||
| param_server_id = validate_path_param("server_id", server_id) | ||
|
|
||
| res = self._request( | ||
| "GET", | ||
| f"/instance/v1/zones/{param_zone}/servers/{param_server_id}/user_data/{key}", | ||
| body=marshal_GetServerUserDataRequest( | ||
| GetServerUserDataRequest( | ||
| zone=zone, | ||
| server_id=server_id, | ||
| key=key, | ||
| ), | ||
| self.client, | ||
| ), | ||
| ) | ||
| self._throw_on_error(res) | ||
| return res | ||
|
|
||
| def set_server_user_data( | ||
| self, server_id: str, key: str, content: bytes, zone: Optional[ScwZone] = None | ||
| ) -> Response: | ||
| """ | ||
| Sets the content of a user data on a server for the given key. | ||
| :param zone: Zone to target. If none is passed, it will use the default zone from the config. | ||
| :param server_id: The ID of the server. | ||
| :param key: The user data key. | ||
| :param content: The content to set as user data in bytes. | ||
| :return: A plain text response confirming the operation. | ||
| """ | ||
| param_zone = validate_path_param("zone", zone or self.client.default_zone) | ||
| param_server_id = validate_path_param("server_id", server_id) | ||
| headers = { | ||
| "Content-Type": "text/plain", | ||
| } | ||
| res = self._request( | ||
| "PATCH", | ||
| f"/instance/v1/zones/{param_zone}/servers/{param_server_id}/user_data/{key}", | ||
| body=content, | ||
| headers=headers, | ||
| ) | ||
|
|
||
| self._throw_on_error(res) | ||
| return res | ||
|
|
||
| def get_all_server_user_data( | ||
| self, server_id: str, zone: Optional[ScwZone] = None | ||
| ) -> GetAllServerUserDataResponse: | ||
| param_zone = validate_path_param("zone", zone or self.client.default_zone) | ||
| param_server_id = validate_path_param("server_id", server_id) | ||
|
|
||
| all_user_data_res = InstanceUtilsV1API.list_server_user_data( | ||
| self, server_id=param_server_id, zone=param_zone | ||
| ) | ||
|
|
||
| user_data: Dict[str, bytes] = {} | ||
| for key in all_user_data_res.user_data: | ||
| value = InstanceUtilsV1API.get_server_user_data( | ||
| self, server_id=param_server_id, key=key | ||
| ) | ||
| print("value: ", value) | ||
| user_data[key] = value.content | ||
|
|
||
| res = GetAllServerUserDataResponse(user_data=user_data) | ||
|
|
||
| return res | ||
|
|
||
| def set_all_server_user_data( | ||
| self, | ||
| server_id: str, | ||
| user_data: Dict[str, bytes], | ||
| zone: Optional[ScwZone] = None, | ||
| ) -> Optional[None]: | ||
| param_zone = validate_path_param("zone", zone or self.client.default_zone) | ||
| param_server_id = validate_path_param("server_id", server_id) | ||
|
|
||
| all_user_data_res = InstanceUtilsV1API.list_server_user_data( | ||
| self, server_id=param_server_id, zone=param_zone | ||
| ) | ||
| for key in all_user_data_res.user_data: | ||
| if user_data.get(key) is not None: | ||
| continue | ||
| InstanceUtilsV1API.delete_server_user_data( | ||
| self, server_id=param_server_id, key=key | ||
| ) | ||
|
|
||
| for key in user_data: | ||
| InstanceUtilsV1API.set_server_user_data( | ||
| self, | ||
| server_id=param_server_id, | ||
| zone=param_zone, | ||
| key=key, | ||
| content=user_data[key], | ||
| ) | ||
|
|
||
| return None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| from typing import Dict, Any | ||
|
|
||
| from scaleway.instance.v1.custom_types import ( | ||
| GetServerUserDataRequest, | ||
| GetAllServerUserDataRequest, | ||
| ) | ||
| from scaleway_core.profile import ProfileDefaults | ||
|
|
||
|
|
||
| def marshal_GetServerUserDataRequest( | ||
| request: GetServerUserDataRequest, defaults: ProfileDefaults | ||
| ) -> Dict[str, Any]: | ||
| output: Dict[str, Any] = {} | ||
|
|
||
| if request.server_id is not None: | ||
| output["server_id"] = request.server_id | ||
| if request.key is not None: | ||
| output["key"] = request.key | ||
| if request.zone is not None: | ||
| output["zone"] = request.zone | ||
|
|
||
| return output | ||
|
|
||
|
|
||
| def marshal_ListServerUserDataRequest( | ||
| request: GetAllServerUserDataRequest, defaults: ProfileDefaults | ||
| ) -> Dict[str, Any]: | ||
| output: Dict[str, Any] = {} | ||
|
|
||
| if request.server_id is not None: | ||
| output["server_id"] = request.server_id | ||
| if request.zone is not None: | ||
| output["zone"] = request.zone | ||
|
|
||
| return output |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| from dataclasses import dataclass | ||
| from typing import Optional, Dict | ||
|
|
||
| from scaleway_core.bridge import Zone as ScwZone | ||
|
|
||
|
|
||
| @dataclass | ||
| class GetServerUserDataRequest: | ||
| zone: Optional[ScwZone] | ||
| """ | ||
| Zone of the user data to get | ||
| """ | ||
|
|
||
| server_id: str | ||
|
|
||
| key: str | ||
| """ | ||
| Key defines the user data key to get | ||
| """ | ||
|
|
||
|
|
||
| @dataclass | ||
| class GetAllServerUserDataRequest: | ||
| zone: Optional[ScwZone] | ||
| """ | ||
| Zone of the user data to get | ||
| """ | ||
|
|
||
| server_id: str | ||
|
|
||
|
|
||
| @dataclass | ||
| class GetAllServerUserDataResponse: | ||
| user_data: Dict[str, bytes] | ||
|
|
||
|
|
||
| @dataclass | ||
| class SetAllServerUserDataRequest: | ||
| zone: Optional[ScwZone] | ||
| """ | ||
| Zone of the user data to set | ||
| """ | ||
|
|
||
| server_id: str | ||
|
|
||
| user_data: Dict[str, bytes] | ||
Laure-di marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
Empty file.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.