InstanceGroup should add/remove instances (#82)

This commit is contained in:
The Magician 2018-08-30 13:31:49 -07:00 committed by Alex Stephen
commit be863bd64e
2 changed files with 160 additions and 142 deletions

View file

@ -79,26 +79,6 @@ options:
description: description:
- The port number, which can be a value between 1 and 65535. - The port number, which can be a value between 1 and 65535.
required: false 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: zone:
description: description:
- A reference to the zone where the instance group resides. - A reference to the zone where the instance group resides.
@ -107,18 +87,18 @@ options:
description: description:
- The list of instances associated with this InstanceGroup. - The list of instances associated with this InstanceGroup.
- All instances must be created before being added to an 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 - All instances not in this list will be removed from the InstanceGroup and will not
not be deleted. be deleted.
- Only the full identifier of the instance will be returned. - Only the full identifier of the instance will be returned.
required: false required: false
version_added: 2.8 version_added: 2.7
extends_documentation_fragment: gcp extends_documentation_fragment: gcp
''' '''
EXAMPLES = ''' EXAMPLES = '''
- name: create a network - name: create a network
gcp_compute_network: gcp_compute_network:
name: "network-instancegroup" name: network-instancegroup
project: "{{ gcp_project }}" project: "{{ gcp_project }}"
auth_kind: "{{ gcp_cred_kind }}" auth_kind: "{{ gcp_cred_kind }}"
service_account_file: "{{ gcp_cred_file }}" service_account_file: "{{ gcp_cred_file }}"
@ -127,14 +107,14 @@ EXAMPLES = '''
- name: create a instance group - name: create a instance group
gcp_compute_instance_group: gcp_compute_instance_group:
name: "test_object" name: test_object
named_ports: named_ports:
- name: ansible - name: ansible
port: 1234 port: 1234
network: "{{ network }}" network: "{{ network }}"
zone: us-central1-a zone: us-central1-a
project: "test_project" project: test_project
auth_kind: "serviceaccount" auth_kind: serviceaccount
service_account_file: "/tmp/auth.pem" service_account_file: "/tmp/auth.pem"
state: present state: present
''' '''
@ -163,6 +143,23 @@ name:
returned: success returned: success
type: str type: str
namedPorts: namedPorts:
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 of the instance group.
- The name must be 1-63 characters long, and comply with RFC1035.
returned: success
type: str
named_ports:
description: description:
- Assigns a name to a port number. - Assigns a name to a port number.
- 'For example: {name: "http", port: 80}.' - 'For example: {name: "http", port: 80}.'
@ -188,7 +185,7 @@ network:
description: description:
- The network to which all instances in the instance group belong. - The network to which all instances in the instance group belong.
returned: success returned: success
type: str type: dict
region: region:
description: description:
- The region where the instance group is located (for regional resources). - The region where the instance group is located (for regional resources).
@ -198,7 +195,7 @@ subnetwork:
description: description:
- The subnetwork to which all instances in the instance group belong. - The subnetwork to which all instances in the instance group belong.
returned: success returned: success
type: str type: dict
zone: zone:
description: description:
- A reference to the zone where the instance group resides. - A reference to the zone where the instance group resides.
@ -208,8 +205,8 @@ instances:
description: description:
- The list of instances associated with this InstanceGroup. - The list of instances associated with this InstanceGroup.
- All instances must be created before being added to an 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 - All instances not in this list will be removed from the InstanceGroup and will not
not be deleted. be deleted.
- Only the full identifier of the instance will be returned. - Only the full identifier of the instance will be returned.
returned: success returned: success
type: list type: list
@ -238,11 +235,11 @@ def main():
description=dict(type='str'), description=dict(type='str'),
name=dict(type='str'), name=dict(type='str'),
named_ports=dict(type='list', elements='dict', options=dict(name=dict(type='str'), port=dict(type='int'))), 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'), region=dict(type='str'),
subnetwork=dict(), subnetwork=dict(type='dict'),
zone=dict(required=True, type='str'), 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): def list_instances(self):
auth = GcpSession(self.module, 'compute') 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 # Transform instance list into a list of selfLinks for diffing with module parameters
instances = [] 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) return "https://www.googleapis.com/compute/v1/projects/{project}/zones/{zone}/instanceGroups/{name}/addInstances".format(**self.module.params)
def _build_request(self, instances): def _build_request(self, instances):
request = {'instances': []} request = {
'instances': []
}
for instance in instances: for instance in instances:
request['instances'].append({'instance': instance}) request['instances'].append({'instance': instance})
return request return request
class InstanceGroupNamedportsArray(object): class InstanceGroupNamedPortsArray(object):
def __init__(self, request, module): def __init__(self, request, module):
self.module = module self.module = module
if request: if request:

View file

@ -53,7 +53,7 @@ extends_documentation_fragment: gcp
''' '''
EXAMPLES = ''' EXAMPLES = '''
- name: a instance group facts - name: " a instance group facts"
gcp_compute_instance_group_facts: gcp_compute_instance_group_facts:
zone: us-central1-a zone: us-central1-a
filters: filters:
@ -61,11 +61,12 @@ EXAMPLES = '''
project: test_project project: test_project
auth_kind: serviceaccount auth_kind: serviceaccount
service_account_file: "/tmp/auth.pem" service_account_file: "/tmp/auth.pem"
state: facts
''' '''
RETURN = ''' RETURN = '''
items: resources:
description: List of items description: List of resources
returned: always returned: always
type: complex type: complex
contains: contains:
@ -101,6 +102,23 @@ items:
ports apply to all instances in this instance group.' ports apply to all instances in this instance group.'
returned: success returned: success
type: complex type: complex
contains:
name:
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: contains:
name: name:
description: description:
@ -117,7 +135,7 @@ items:
description: description:
- The network to which all instances in the instance group belong. - The network to which all instances in the instance group belong.
returned: success returned: success
type: str type: dict
region: region:
description: description:
- The region where the instance group is located (for regional resources). - The region where the instance group is located (for regional resources).
@ -127,7 +145,7 @@ items:
description: description:
- The subnetwork to which all instances in the instance group belong. - The subnetwork to which all instances in the instance group belong.
returned: success returned: success
type: str type: dict
zone: zone:
description: description:
- A reference to the zone where the instance group resides. - A reference to the zone where the instance group resides.
@ -137,8 +155,8 @@ items:
description: description:
- The list of instances associated with this InstanceGroup. - The list of instances associated with this InstanceGroup.
- All instances must be created before being added to an 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 - All instances not in this list will be removed from the InstanceGroup and will not
will not be deleted. be deleted.
- Only the full identifier of the instance will be returned. - Only the full identifier of the instance will be returned.
returned: success returned: success
type: list type: list
@ -166,7 +184,7 @@ def main():
items = items.get('items') items = items.get('items')
else: else:
items = [] items = []
return_value = {'items': items} return_value = {'resources': items}
module.exit_json(**return_value) module.exit_json(**return_value)