mirror of
https://github.com/ansible-collections/google.cloud.git
synced 2025-04-06 10:50:28 -07:00
InstanceGroup should add/remove instances (#82)
This commit is contained in:
parent
073ca550e5
commit
be863bd64e
2 changed files with 160 additions and 142 deletions
|
@ -79,64 +79,44 @@ options:
|
|||
description:
|
||||
- The port number, which can be a value between 1 and 65535.
|
||||
required: false
|
||||
network:
|
||||
description:
|
||||
- The network to which all instances in the instance group belong.
|
||||
- 'This field represents a link to a Network resource in GCP. It can be specified
|
||||
in two ways. First, you can place in the selfLink of the resource here as a
|
||||
string Alternatively, you can add `register: name-of-resource` to a gcp_compute_network
|
||||
task and then set this network field to "{{ name-of-resource }}"'
|
||||
required: false
|
||||
region:
|
||||
description:
|
||||
- The region where the instance group is located (for regional resources).
|
||||
required: false
|
||||
subnetwork:
|
||||
description:
|
||||
- The subnetwork to which all instances in the instance group belong.
|
||||
- 'This field represents a link to a Subnetwork resource in GCP. It can be specified
|
||||
in two ways. First, you can place in the selfLink of the resource here as a
|
||||
string Alternatively, you can add `register: name-of-resource` to a gcp_compute_subnetwork
|
||||
task and then set this subnetwork field to "{{ name-of-resource }}"'
|
||||
required: false
|
||||
zone:
|
||||
description:
|
||||
- A reference to the zone where the instance group resides.
|
||||
required: true
|
||||
instances:
|
||||
description:
|
||||
- The list of instances associated with this InstanceGroup.
|
||||
- All instances must be created before being added to an InstanceGroup.
|
||||
- All instances not in this list will be removed from the InstanceGroup and will
|
||||
not be deleted.
|
||||
- Only the full identifier of the instance will be returned.
|
||||
required: false
|
||||
version_added: 2.8
|
||||
zone:
|
||||
description:
|
||||
- A reference to the zone where the instance group resides.
|
||||
required: true
|
||||
instances:
|
||||
description:
|
||||
- The list of instances associated with this InstanceGroup.
|
||||
- All instances must be created before being added to an InstanceGroup.
|
||||
- All instances not in this list will be removed from the InstanceGroup and will not
|
||||
be deleted.
|
||||
- Only the full identifier of the instance will be returned.
|
||||
required: false
|
||||
version_added: 2.7
|
||||
extends_documentation_fragment: gcp
|
||||
'''
|
||||
|
||||
EXAMPLES = '''
|
||||
- name: create a network
|
||||
gcp_compute_network:
|
||||
name: "network-instancegroup"
|
||||
project: "{{ gcp_project }}"
|
||||
auth_kind: "{{ gcp_cred_kind }}"
|
||||
service_account_file: "{{ gcp_cred_file }}"
|
||||
state: present
|
||||
name: network-instancegroup
|
||||
project: "{{ gcp_project }}"
|
||||
auth_kind: "{{ gcp_cred_kind }}"
|
||||
service_account_file: "{{ gcp_cred_file }}"
|
||||
state: present
|
||||
register: network
|
||||
|
||||
- name: create a instance group
|
||||
gcp_compute_instance_group:
|
||||
name: "test_object"
|
||||
named_ports:
|
||||
- name: ansible
|
||||
port: 1234
|
||||
network: "{{ network }}"
|
||||
zone: us-central1-a
|
||||
project: "test_project"
|
||||
auth_kind: "serviceaccount"
|
||||
service_account_file: "/tmp/auth.pem"
|
||||
state: present
|
||||
name: test_object
|
||||
named_ports:
|
||||
- name: ansible
|
||||
port: 1234
|
||||
network: "{{ network }}"
|
||||
zone: us-central1-a
|
||||
project: test_project
|
||||
auth_kind: serviceaccount
|
||||
service_account_file: "/tmp/auth.pem"
|
||||
state: present
|
||||
'''
|
||||
|
||||
RETURN = '''
|
||||
|
@ -174,45 +154,62 @@ namedPorts:
|
|||
type: complex
|
||||
contains:
|
||||
name:
|
||||
description:
|
||||
- The name for this named port.
|
||||
- The name must be 1-63 characters long, and comply with RFC1035.
|
||||
returned: success
|
||||
type: str
|
||||
port:
|
||||
description:
|
||||
- The port number, which can be a value between 1 and 65535.
|
||||
returned: success
|
||||
type: int
|
||||
network:
|
||||
description:
|
||||
- The network to which all instances in the instance group belong.
|
||||
returned: success
|
||||
type: str
|
||||
region:
|
||||
description:
|
||||
- The region where the instance group is located (for regional resources).
|
||||
returned: success
|
||||
type: str
|
||||
subnetwork:
|
||||
description:
|
||||
- The subnetwork to which all instances in the instance group belong.
|
||||
returned: success
|
||||
type: str
|
||||
zone:
|
||||
description:
|
||||
- A reference to the zone where the instance group resides.
|
||||
returned: success
|
||||
type: str
|
||||
instances:
|
||||
description:
|
||||
- The list of instances associated with this InstanceGroup.
|
||||
- All instances must be created before being added to an InstanceGroup.
|
||||
- All instances not in this list will be removed from the InstanceGroup and will
|
||||
not be deleted.
|
||||
- Only the full identifier of the instance will be returned.
|
||||
returned: success
|
||||
type: list
|
||||
description:
|
||||
- The name of the instance group.
|
||||
- The name must be 1-63 characters long, and comply with RFC1035.
|
||||
returned: success
|
||||
type: str
|
||||
named_ports:
|
||||
description:
|
||||
- Assigns a name to a port number.
|
||||
- 'For example: {name: "http", port: 80}.'
|
||||
- This allows the system to reference ports by the assigned name instead of a port
|
||||
number. Named ports can also contain multiple ports.
|
||||
- 'For example: [{name: "http", port: 80},{name: "http", port: 8080}] Named ports
|
||||
apply to all instances in this instance group.'
|
||||
returned: success
|
||||
type: complex
|
||||
contains:
|
||||
name:
|
||||
description:
|
||||
- The name for this named port.
|
||||
- The name must be 1-63 characters long, and comply with RFC1035.
|
||||
returned: success
|
||||
type: str
|
||||
port:
|
||||
description:
|
||||
- The port number, which can be a value between 1 and 65535.
|
||||
returned: success
|
||||
type: int
|
||||
network:
|
||||
description:
|
||||
- The network to which all instances in the instance group belong.
|
||||
returned: success
|
||||
type: dict
|
||||
region:
|
||||
description:
|
||||
- The region where the instance group is located (for regional resources).
|
||||
returned: success
|
||||
type: str
|
||||
subnetwork:
|
||||
description:
|
||||
- The subnetwork to which all instances in the instance group belong.
|
||||
returned: success
|
||||
type: dict
|
||||
zone:
|
||||
description:
|
||||
- A reference to the zone where the instance group resides.
|
||||
returned: success
|
||||
type: str
|
||||
instances:
|
||||
description:
|
||||
- The list of instances associated with this InstanceGroup.
|
||||
- All instances must be created before being added to an InstanceGroup.
|
||||
- All instances not in this list will be removed from the InstanceGroup and will not
|
||||
be deleted.
|
||||
- Only the full identifier of the instance will be returned.
|
||||
returned: success
|
||||
type: list
|
||||
'''
|
||||
|
||||
################################################################################
|
||||
|
@ -238,11 +235,11 @@ def main():
|
|||
description=dict(type='str'),
|
||||
name=dict(type='str'),
|
||||
named_ports=dict(type='list', elements='dict', options=dict(name=dict(type='str'), port=dict(type='int'))),
|
||||
network=dict(),
|
||||
network=dict(type='dict'),
|
||||
region=dict(type='str'),
|
||||
subnetwork=dict(),
|
||||
subnetwork=dict(type='dict'),
|
||||
zone=dict(required=True, type='str'),
|
||||
instances=dict(type='list'),
|
||||
instances=dict(type='list', elements='dict')
|
||||
)
|
||||
)
|
||||
|
||||
|
@ -450,7 +447,8 @@ class InstanceLogic(object):
|
|||
|
||||
def list_instances(self):
|
||||
auth = GcpSession(self.module, 'compute')
|
||||
response = return_if_object(self.module, auth.post(self._list_instances_url(), {'instanceState': 'ALL'}), 'compute#instanceGroupsListInstances')
|
||||
response = return_if_object(self.module, auth.post(self._list_instances_url(), {'instanceState': 'ALL'}),
|
||||
'compute#instanceGroupsListInstances')
|
||||
|
||||
# Transform instance list into a list of selfLinks for diffing with module parameters
|
||||
instances = []
|
||||
|
@ -476,13 +474,15 @@ class InstanceLogic(object):
|
|||
return "https://www.googleapis.com/compute/v1/projects/{project}/zones/{zone}/instanceGroups/{name}/addInstances".format(**self.module.params)
|
||||
|
||||
def _build_request(self, instances):
|
||||
request = {'instances': []}
|
||||
request = {
|
||||
'instances': []
|
||||
}
|
||||
for instance in instances:
|
||||
request['instances'].append({'instance': instance})
|
||||
return request
|
||||
|
||||
|
||||
class InstanceGroupNamedportsArray(object):
|
||||
class InstanceGroupNamedPortsArray(object):
|
||||
def __init__(self, request, module):
|
||||
self.module = module
|
||||
if request:
|
||||
|
|
|
@ -53,19 +53,20 @@ extends_documentation_fragment: gcp
|
|||
'''
|
||||
|
||||
EXAMPLES = '''
|
||||
- name: a instance group facts
|
||||
- name: " a instance group facts"
|
||||
gcp_compute_instance_group_facts:
|
||||
zone: us-central1-a
|
||||
filters:
|
||||
- name = test_object
|
||||
project: test_project
|
||||
auth_kind: serviceaccount
|
||||
service_account_file: "/tmp/auth.pem"
|
||||
zone: us-central1-a
|
||||
filters:
|
||||
- name = test_object
|
||||
project: test_project
|
||||
auth_kind: serviceaccount
|
||||
service_account_file: "/tmp/auth.pem"
|
||||
state: facts
|
||||
'''
|
||||
|
||||
RETURN = '''
|
||||
items:
|
||||
description: List of items
|
||||
resources:
|
||||
description: List of resources
|
||||
returned: always
|
||||
type: complex
|
||||
contains:
|
||||
|
@ -103,45 +104,62 @@ items:
|
|||
type: complex
|
||||
contains:
|
||||
name:
|
||||
description:
|
||||
- The name for this named port.
|
||||
- The name must be 1-63 characters long, and comply with RFC1035.
|
||||
returned: success
|
||||
type: str
|
||||
port:
|
||||
description:
|
||||
- The port number, which can be a value between 1 and 65535.
|
||||
returned: success
|
||||
type: int
|
||||
network:
|
||||
description:
|
||||
- The network to which all instances in the instance group belong.
|
||||
returned: success
|
||||
type: str
|
||||
region:
|
||||
description:
|
||||
- The region where the instance group is located (for regional resources).
|
||||
returned: success
|
||||
type: str
|
||||
subnetwork:
|
||||
description:
|
||||
- The subnetwork to which all instances in the instance group belong.
|
||||
returned: success
|
||||
type: str
|
||||
zone:
|
||||
description:
|
||||
- A reference to the zone where the instance group resides.
|
||||
returned: success
|
||||
type: str
|
||||
instances:
|
||||
description:
|
||||
- The list of instances associated with this InstanceGroup.
|
||||
- All instances must be created before being added to an InstanceGroup.
|
||||
- All instances not in this list will be removed from the InstanceGroup and
|
||||
will not be deleted.
|
||||
- Only the full identifier of the instance will be returned.
|
||||
returned: success
|
||||
type: list
|
||||
description:
|
||||
- The name of the instance group.
|
||||
- The name must be 1-63 characters long, and comply with RFC1035.
|
||||
returned: success
|
||||
type: str
|
||||
named_ports:
|
||||
description:
|
||||
- Assigns a name to a port number.
|
||||
- 'For example: {name: "http", port: 80}.'
|
||||
- This allows the system to reference ports by the assigned name instead of a port
|
||||
number. Named ports can also contain multiple ports.
|
||||
- 'For example: [{name: "http", port: 80},{name: "http", port: 8080}] Named ports
|
||||
apply to all instances in this instance group.'
|
||||
returned: success
|
||||
type: complex
|
||||
contains:
|
||||
name:
|
||||
description:
|
||||
- The name for this named port.
|
||||
- The name must be 1-63 characters long, and comply with RFC1035.
|
||||
returned: success
|
||||
type: str
|
||||
port:
|
||||
description:
|
||||
- The port number, which can be a value between 1 and 65535.
|
||||
returned: success
|
||||
type: int
|
||||
network:
|
||||
description:
|
||||
- The network to which all instances in the instance group belong.
|
||||
returned: success
|
||||
type: dict
|
||||
region:
|
||||
description:
|
||||
- The region where the instance group is located (for regional resources).
|
||||
returned: success
|
||||
type: str
|
||||
subnetwork:
|
||||
description:
|
||||
- The subnetwork to which all instances in the instance group belong.
|
||||
returned: success
|
||||
type: dict
|
||||
zone:
|
||||
description:
|
||||
- A reference to the zone where the instance group resides.
|
||||
returned: success
|
||||
type: str
|
||||
instances:
|
||||
description:
|
||||
- The list of instances associated with this InstanceGroup.
|
||||
- All instances must be created before being added to an InstanceGroup.
|
||||
- All instances not in this list will be removed from the InstanceGroup and will not
|
||||
be deleted.
|
||||
- Only the full identifier of the instance will be returned.
|
||||
returned: success
|
||||
type: list
|
||||
'''
|
||||
|
||||
################################################################################
|
||||
|
@ -166,7 +184,7 @@ def main():
|
|||
items = items.get('items')
|
||||
else:
|
||||
items = []
|
||||
return_value = {'items': items}
|
||||
return_value = {'resources': items}
|
||||
module.exit_json(**return_value)
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue