Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ansible InstanceGroup should add/remove instances #424

Merged
merged 5 commits into from
Aug 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build/terraform
15 changes: 15 additions & 0 deletions products/compute/ansible.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,21 @@ overrides: !ruby/object:Provider::ResourceOverrides
timeoutSec: !ruby/object:Provider::Ansible::PropertyOverride
aliases:
- timeout_seconds
InstanceGroup: !ruby/object:Provider::Ansible::ResourceOverride
properties:
instances: !ruby/object:Provider::Ansible::PropertyOverride
version_added: '2.7'
exclude: false
update: |
instance = InstanceLogic(module)
instance.run()
post_action: |
if fetch:
instance = InstanceLogic(module)
instance.run()
fetch.update({'instances': instance.list_instances()})
provider_helpers:
- 'products/compute/helpers/python/instancegroup_instances.py'
Instance: !ruby/object:Provider::Ansible::ResourceOverride
provider_helpers:
- 'products/compute/helpers/python/provider_instance.py'
Expand Down
14 changes: 14 additions & 0 deletions products/compute/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1866,6 +1866,20 @@ objects:
imports: 'name'
description: 'A reference to the zone where the instance group resides.'
required: true
- !ruby/object:Api::Type::Array
name: '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.
exclude: true
item_type: !ruby/object:Api::Type::ResourceRef
name: 'instance'
description: 'An instance being added to the InstanceGroup'
resource: 'Instance'
imports: 'selfLink'
properties:
- !ruby/object:Api::Type::Time
name: 'creationTimestamp'
Expand Down
64 changes: 64 additions & 0 deletions products/compute/helpers/python/instancegroup_instances.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<%
# Instance Logic handling for Instance Groups
# This code handles the adding + removing of instances from an instance group.
# It should be run after all normal create/update/delete logic.

-%>
class InstanceLogic(object):
def __init__(self, module):
self.module = module
self.current_instances = self.list_instances()
self.module_instances = []

# Transform module list of instances (dicts of instance responses) into a list of selfLinks.
instances = self.module.params.get('instances')
if instances:
for instance in instances:
self.module_instances.append(replace_resource_dict(instance, 'selfLink'))

def run(self):
# Find all instances to add and add them
instances_to_add = list(set(self.module_instances) - set(self.current_instances))
if instances_to_add:
self.add_instances(instances_to_add)

# Find all instances to remove and remove them
instances_to_remove = list(set(self.current_instances) - set(self.module_instances))
if instances_to_remove:
self.remove_instances(instances_to_remove)

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')

# Transform instance list into a list of selfLinks for diffing with module parameters
instances = []
for instance in response.get('items', []):
instances.append(instance['instance'])
return instances

def add_instances(self, instances):
auth = GcpSession(self.module, 'compute')
wait_for_operation(self.module, auth.post(self._add_instances_url(), self._build_request(instances)))

def remove_instances(self, instances):
auth = GcpSession(self.module, 'compute')
wait_for_operation(self.module, auth.post(self._remove_instances_url(), self._build_request(instances)))

def _list_instances_url(self):
return "https://www.googleapis.com/compute/v1/projects/{project}/zones/{zone}/instanceGroups/{name}/listInstances".format(**self.module.params)

def _remove_instances_url(self):
return "https://www.googleapis.com/compute/v1/projects/{project}/zones/{zone}/instanceGroups/{name}/removeInstances".format(**self.module.params)

def _add_instances_url(self):
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': []
}
for instance in instances:
request['instances'].append({'instance': instance})
return request
2 changes: 2 additions & 0 deletions provider/ansible/resource_override.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ module OverrideProperties
attr_reader :hidden
attr_reader :imports
attr_reader :post_create
attr_reader :post_action
attr_reader :provider_helpers
attr_reader :return_if_object
attr_reader :unwrap_resource
Expand Down Expand Up @@ -60,6 +61,7 @@ def validate
check_optional_property :hidden, ::Array
check_property :imports, ::Array
check_optional_property :post_create, ::String
check_optional_property :post_action, ::String
check_property :provider_helpers, ::Array
check_optional_property :return_if_object, ::String
check_optional_property :update, ::String
Expand Down
3 changes: 3 additions & 0 deletions templates/ansible/resource.erb
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@ def main():
else:
fetch = {}

<% if object.post_action -%>
<%= lines(indent(object.post_action, 4)) -%>
<% end # ifobject.post_create -%>
fetch.update({'changed': changed})
<% end # object.readonly -%>

Expand Down