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
parent 073ca550e5
commit be863bd64e
2 changed files with 160 additions and 142 deletions

View file

@ -79,64 +79,44 @@ 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: zone:
description: description:
- The network to which all instances in the instance group belong. - A reference to the zone where the instance group resides.
- 'This field represents a link to a Network resource in GCP. It can be specified required: true
in two ways. First, you can place in the selfLink of the resource here as a instances:
string Alternatively, you can add `register: name-of-resource` to a gcp_compute_network description:
task and then set this network field to "{{ name-of-resource }}"' - The list of instances associated with this InstanceGroup.
required: false - All instances must be created before being added to an InstanceGroup.
region: - All instances not in this list will be removed from the InstanceGroup and will not
description: be deleted.
- The region where the instance group is located (for regional resources). - Only the full identifier of the instance will be returned.
required: false required: false
subnetwork: version_added: 2.7
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
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 }}"
state: present state: present
register: network register: network
- 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
''' '''
RETURN = ''' RETURN = '''
@ -174,45 +154,62 @@ namedPorts:
type: complex type: complex
contains: contains:
name: name:
description: description:
- The name for this named port. - The name of the instance group.
- The name must be 1-63 characters long, and comply with RFC1035. - The name must be 1-63 characters long, and comply with RFC1035.
returned: success returned: success
type: str type: str
port: named_ports:
description: description:
- The port number, which can be a value between 1 and 65535. - Assigns a name to a port number.
returned: success - 'For example: {name: "http", port: 80}.'
type: int - This allows the system to reference ports by the assigned name instead of a port
network: number. Named ports can also contain multiple ports.
description: - 'For example: [{name: "http", port: 80},{name: "http", port: 8080}] Named ports
- The network to which all instances in the instance group belong. apply to all instances in this instance group.'
returned: success returned: success
type: str type: complex
region: contains:
description: name:
- The region where the instance group is located (for regional resources). description:
returned: success - The name for this named port.
type: str - The name must be 1-63 characters long, and comply with RFC1035.
subnetwork: returned: success
description: type: str
- The subnetwork to which all instances in the instance group belong. port:
returned: success description:
type: str - The port number, which can be a value between 1 and 65535.
zone: returned: success
description: type: int
- A reference to the zone where the instance group resides. network:
returned: success description:
type: str - The network to which all instances in the instance group belong.
instances: returned: success
description: type: dict
- The list of instances associated with this InstanceGroup. region:
- All instances must be created before being added to an InstanceGroup. description:
- All instances not in this list will be removed from the InstanceGroup and will - The region where the instance group is located (for regional resources).
not be deleted. returned: success
- Only the full identifier of the instance will be returned. type: str
returned: success subnetwork:
type: list 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'), 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,19 +53,20 @@ 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:
- name = test_object - name = test_object
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:
@ -103,45 +104,62 @@ items:
type: complex type: complex
contains: contains:
name: name:
description: description:
- The name for this named port. - The name of the instance group.
- The name must be 1-63 characters long, and comply with RFC1035. - The name must be 1-63 characters long, and comply with RFC1035.
returned: success returned: success
type: str type: str
port: named_ports:
description: description:
- The port number, which can be a value between 1 and 65535. - Assigns a name to a port number.
returned: success - 'For example: {name: "http", port: 80}.'
type: int - This allows the system to reference ports by the assigned name instead of a port
network: number. Named ports can also contain multiple ports.
description: - 'For example: [{name: "http", port: 80},{name: "http", port: 8080}] Named ports
- The network to which all instances in the instance group belong. apply to all instances in this instance group.'
returned: success returned: success
type: str type: complex
region: contains:
description: name:
- The region where the instance group is located (for regional resources). description:
returned: success - The name for this named port.
type: str - The name must be 1-63 characters long, and comply with RFC1035.
subnetwork: returned: success
description: type: str
- The subnetwork to which all instances in the instance group belong. port:
returned: success description:
type: str - The port number, which can be a value between 1 and 65535.
zone: returned: success
description: type: int
- A reference to the zone where the instance group resides. network:
returned: success description:
type: str - The network to which all instances in the instance group belong.
instances: returned: success
description: type: dict
- The list of instances associated with this InstanceGroup. region:
- All instances must be created before being added to an InstanceGroup. description:
- All instances not in this list will be removed from the InstanceGroup and - The region where the instance group is located (for regional resources).
will not be deleted. returned: success
- Only the full identifier of the instance will be returned. type: str
returned: success subnetwork:
type: list 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') 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)