Debian Codegraph @

ansible_collections/azure/azcollection/plugins/modules/azure_rm_sshpublickey_info.py

raw symbols SCIP
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2023 xuzhang3 (@xuzhang3), Fred-sun (@Fred-sun)
4 #
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 from __future__ import absolute_import, division, print_function
8 __metaclass__ = type
9
10
11 DOCUMENTATION = '''
12 ---
13 module: azure_rm_sshpublickey_info
14
15 version_added: "2.0.0"
16
17 short_description: Get Ssh Public Key with VM facts
18
19 description:
20 - Get Ssh Public Key with VM facts
21
22 options:
23 resource_group:
24 description:
25 - Name of the resource group.
26 type: str
27 name:
28 description:
29 - Name of the SSH Public Key.
30 type: str
31 tags:
32 description:
33 - Limit results by providing a list of tags. Format tags as 'key' or 'key:value'.
34 type: list
35 elements: str
36
37 extends_documentation_fragment:
38 - azure.azcollection.azure
39
40 author:
41 - xuzhang3 (@xuzhang3)
42 - Fred-sun (@Fred-sun)
43
44 '''
45
46 EXAMPLES = '''
47 - name: Get facts of the VM's ssh public key by name
48 azure_rm_sshpublickey_info:
49 resource_group: myResourceGroup
50 name: mysshpublickey
51
52 - name: Get facts of the VM's ssh public key by resource group
53 azure_rm_sshpublickey_info:
54 resource_group: myResourceGroup
55
56 - name: Get facts by tags
57 azure_rm_sshpublickey_info:
58 resource_group: myResourceGroup
59 tags:
60 - testing
61 - foo:bar
62 '''
63
64 RETURN = '''
65 ssh_keys:
66 description:
67 - Current state of the SSH Public Key.
68 returned: always
69 type: complex
70 contains:
71 id:
72 description:
73 - Resource ID.
74 returned: always
75 type: str
76 sample: /subscriptions/xxxx/resourceGroups/xxx/providers/Microsoft.Compute/sshPublicKeys/mySshPublicKeyName
77 location:
78 description:
79 - The Geo-location where the resource lives.
80 returned: always
81 type: str
82 sample: eastus
83 name:
84 description:
85 - Resource name.
86 returned: always
87 type: str
88 sample: mySshPublicKey
89 tags:
90 description:
91 - Resource tags, such as { 'tags1':'value1' }.
92 returned: always
93 type: dict
94 sample: { 'key1':'value1' }
95 public_key:
96 description:
97 - SSH public key used to authenticate to a virtual machine through ssh.
98 returned: always
99 type: str
100 sample: "ssh-rsa **************@test.com"
101 '''
102
103 try:
104 from azure.core.exceptions import ResourceNotFoundError
105 except Exception:
106 # This is handled in azure_rm_common
107 pass
108
109 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase
110
111
112 class AzureRMSshPublicKeyInfo(AzureRMModuleBase):
113
114 def __init__(self):
115
116 self.module_arg_spec = dict(
117 resource_group=dict(type='str'),
118 name=dict(type='str'),
119 tags=dict(type='list', elements='str')
120 )
121
122 self.results = dict(
123 changed=False,
124 ssh_keys=[]
125 )
126
127 self.resource_group = None
128 self.name = None
129 self.tags = None
130
131 super(AzureRMSshPublicKeyInfo, self).__init__(self.module_arg_spec,
132 supports_check_mode=True,
133 supports_tags=False,
134 facts_module=True)
135
136 def exec_module(self, **kwargs):
137 for key in self.module_arg_spec:
138 setattr(self, key, kwargs[key])
139
140 if self.name and self.resource_group:
141 response = [self.get_by_name()]
142 elif self.resource_group:
143 response = self.list_by_resourcegroup()
144 else:
145 response = self.list_all()
146
147 self.results['ssh_keys'] = [self.to_dict(item) for item in response if response is not None]
148
149 return self.results
150
151 def get_by_name(self):
152 response = None
153 try:
154 response = self.compute_client.ssh_public_keys.get(self.resource_group, self.name)
155
156 except ResourceNotFoundError as exec:
157 self.log("Failed to get ssh public keys, Exception as {0}".format(exec))
158
159 return response
160
161 def list_by_resourcegroup(self):
162 response = None
163 try:
164 response = self.compute_client.ssh_public_keys.list_by_resource_group(self.resource_group)
165 except Exception as exec:
166 self.log("Faild to list ssh public keys by resource group, exception as {0}".format(exec))
167 return response
168
169 def list_all(self):
170 response = None
171 try:
172 response = self.compute_client.ssh_public_keys.list_by_subscription()
173 except Exception as exc:
174 self.fail("Failed to list all items - {0}".format(str(exc)))
175
176 return response
177
178 def to_dict(self, body):
179 results = dict()
180 if body is not None and self.has_tags(body.tags, self.tags):
181 results = dict(
182 id=body.id,
183 name=body.name,
184 location=body.location,
185 tags=body.tags,
186 public_key=body.public_key
187 )
188 return results
189
190
191 def main():
192 AzureRMSshPublicKeyInfo()
193
194
195 if __name__ == '__main__':
196 main()