1
- from typing import BinaryIO , Tuple
1
+ from typing import BinaryIO , List , Optional , Tuple , Union
2
2
3
3
import requests
4
4
5
5
from linode_api4 .errors import UnexpectedResponseError
6
6
from linode_api4 .groups import Group
7
- from linode_api4 .objects import Base , Image
7
+ from linode_api4 .objects import Base , Disk , Image
8
8
from linode_api4 .util import drop_null_keys
9
9
10
10
@@ -29,39 +29,45 @@ def __call__(self, *filters):
29
29
"""
30
30
return self .client ._get_and_filter (Image , * filters )
31
31
32
- def create (self , disk , label = None , description = None , cloud_init = False ):
32
+ def create (
33
+ self ,
34
+ disk : Union [Disk , int ],
35
+ label : str = None ,
36
+ description : str = None ,
37
+ cloud_init : bool = False ,
38
+ tags : Optional [List [str ]] = None ,
39
+ ):
33
40
"""
34
41
Creates a new Image from a disk you own.
35
42
36
43
API Documentation: https://techdocs.akamai.com/linode-api/reference/post-image
37
44
38
45
:param disk: The Disk to imagize.
39
- :type disk: Disk or int
46
+ :type disk: Union[ Disk, int]
40
47
:param label: The label for the resulting Image (defaults to the disk's
41
48
label.
42
49
:type label: str
43
50
:param description: The description for the new Image.
44
51
:type description: str
45
52
:param cloud_init: Whether this Image supports cloud-init.
46
53
:type cloud_init: bool
54
+ :param tags: A list of customized tags of this new Image.
55
+ :type tags: Optional[List[str]]
47
56
48
57
:returns: The new Image.
49
58
:rtype: Image
50
59
"""
51
60
params = {
52
61
"disk_id" : disk .id if issubclass (type (disk ), Base ) else disk ,
62
+ "label" : label ,
63
+ "description" : description ,
64
+ "tags" : tags ,
53
65
}
54
66
55
- if label is not None :
56
- params ["label" ] = label
57
-
58
- if description is not None :
59
- params ["description" ] = description
60
-
61
67
if cloud_init :
62
68
params ["cloud_init" ] = cloud_init
63
69
64
- result = self .client .post ("/images" , data = params )
70
+ result = self .client .post ("/images" , data = drop_null_keys ( params ) )
65
71
66
72
if not "id" in result :
67
73
raise UnexpectedResponseError (
@@ -78,6 +84,7 @@ def create_upload(
78
84
region : str ,
79
85
description : str = None ,
80
86
cloud_init : bool = False ,
87
+ tags : Optional [List [str ]] = None ,
81
88
) -> Tuple [Image , str ]:
82
89
"""
83
90
Creates a new Image and returns the corresponding upload URL.
@@ -92,11 +99,18 @@ def create_upload(
92
99
:type description: str
93
100
:param cloud_init: Whether this Image supports cloud-init.
94
101
:type cloud_init: bool
102
+ :param tags: A list of customized tags of this Image.
103
+ :type tags: Optional[List[str]]
95
104
96
105
:returns: A tuple containing the new image and the image upload URL.
97
106
:rtype: (Image, str)
98
107
"""
99
- params = {"label" : label , "region" : region , "description" : description }
108
+ params = {
109
+ "label" : label ,
110
+ "region" : region ,
111
+ "description" : description ,
112
+ "tags" : tags ,
113
+ }
100
114
101
115
if cloud_init :
102
116
params ["cloud_init" ] = cloud_init
@@ -114,7 +128,12 @@ def create_upload(
114
128
return Image (self .client , result_image ["id" ], result_image ), result_url
115
129
116
130
def upload (
117
- self , label : str , region : str , file : BinaryIO , description : str = None
131
+ self ,
132
+ label : str ,
133
+ region : str ,
134
+ file : BinaryIO ,
135
+ description : str = None ,
136
+ tags : Optional [List [str ]] = None ,
118
137
) -> Image :
119
138
"""
120
139
Creates and uploads a new image.
@@ -128,12 +147,16 @@ def upload(
128
147
:param file: The BinaryIO object to upload to the image. This is generally obtained from open("myfile", "rb").
129
148
:param description: The description for the new Image.
130
149
:type description: str
150
+ :param tags: A list of customized tags of this Image.
151
+ :type tags: Optional[List[str]]
131
152
132
153
:returns: The resulting image.
133
154
:rtype: Image
134
155
"""
135
156
136
- image , url = self .create_upload (label , region , description = description )
157
+ image , url = self .create_upload (
158
+ label , region , description = description , tags = tags
159
+ )
137
160
138
161
requests .put (
139
162
url ,
0 commit comments