Skip to content

Commit

Permalink
feat: ✨ adds support for /1.0/instances endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonyra committed Feb 21, 2024
1 parent 1dd697b commit 6e20d71
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions plugins/modules/lxd_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,12 +436,12 @@

# CONFIG_PARAMS is a list of config attribute names.
CONFIG_PARAMS = [
'architecture', 'config', 'devices', 'ephemeral', 'profiles', 'source'
'architecture', 'config', 'devices', 'ephemeral', 'profiles', 'source', 'type'
]

# CONFIG_CREATION_PARAMS is a list of attribute names that are only applied
# on instance creation.
CONFIG_CREATION_PARAMS = ['source']
CONFIG_CREATION_PARAMS = ['source', 'type']


class LXDContainerManagement(object):
Expand All @@ -467,13 +467,6 @@ def __init__(self, module):

self.type = self.module.params['type']

# LXD Rest API provides additional endpoints for creating containers and virtual-machines.
self.api_endpoint = None
if self.type == 'container':
self.api_endpoint = '/1.0/containers'
elif self.type == 'virtual-machine':
self.api_endpoint = '/1.0/virtual-machines'

self.key_file = self.module.params.get('client_key')
if self.key_file is None:
self.key_file = '{0}/.config/lxc/client.key'.format(os.environ['HOME'])
Expand All @@ -499,6 +492,18 @@ def __init__(self, module):
)
except LXDClientException as e:
self.module.fail_json(msg=e.msg)

# LXD (3.19) Rest API provides instances endpoint, failback to containers and virtual-machines
# https://documentation.ubuntu.com/lxd/en/latest/rest-api/#instances-containers-and-virtual-machines
self.api_endpoint = '/1.0/instances'
check_api_endpoint = self.client.do('GET', '{0}?project='.format(self.api_endpoint), ok_error_codes=[404])

if check_api_endpoint['error_code'] == 404:
if self.type == 'container':
self.api_endpoint = '/1.0/containers'
elif self.type == 'virtual-machine':
self.api_endpoint = '/1.0/virtual-machines'

self.trust_password = self.module.params.get('trust_password', None)
self.actions = []
self.diff = {'before': {}, 'after': {}}
Expand Down Expand Up @@ -551,6 +556,8 @@ def _create_instance(self):
url = '{0}?{1}'.format(url, urlencode(url_params))
config = self.config.copy()
config['name'] = self.name
if self.type not in self.api_endpoint:
config['type'] = self.type
if not self.module.check_mode:
self.client.do('POST', url, config, wait_for_container=self.wait_for_container)
self.actions.append('create')
Expand Down

0 comments on commit 6e20d71

Please sign in to comment.