Skip to content

Commit

Permalink
RD-6214-clone-add-resize-operation-for-instance-for-gcp-plugin (#194)
Browse files Browse the repository at this point in the history
* stop

* with log

* v1 resize

* add start

* check remove name from utils.py and resize

* resize dont work

* fix to zone input for resize

* resize work!

* resize work!

* fix test

* remove file resize.yaml

* update version

* update v2_plugin, plugin_1_4

* test_resize v1

* test raedy and added in runtime_properties machine_type

Co-authored-by: nelynehemia <nely@cloudify.co>
  • Loading branch information
Nelynehemia and nelynehemia committed Nov 13, 2022
1 parent b7c6b09 commit 728047a
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 14 deletions.
Binary file added .coverage 2
Binary file not shown.
1 change: 1 addition & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
1.8.8: RD-6214 - add resize operation for instance (set_machine_type).
1.8.7: Fix issues from deprecate.
1.8.6: Deprecate old node types.
1.8.5: release redhat8 wagon and dsl 1_4 yaml.
Expand Down
122 changes: 114 additions & 8 deletions cloudify_gcp/compute/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,57 @@ def __init__(self,
self.subnetwork = subnetwork
self.can_ip_forward = can_ip_forward

@utils.sync_operation
@check_response
def set_machine_type(self, name, zone, machine_type):
"""
Set machine type GCP instance.
Zone operation.
:return: REST response with operation responsible for the instance
set machine type process and its status
"""
self.logger.info('Set machine type instance {0}'.format(self.name))
full_machine_type = "{0}/machineTypes/{1}".format(
zone, machine_type)
return self.discovery.instances().setMachineType(
project=self.project,
zone=basename(zone),
instance=name,
body={'machineType': full_machine_type}).execute()

@utils.sync_operation
@check_response
def stop(self):
"""
Stop GCP instance.
Zone operation.
:return: REST response with operation responsible for the instance
stop process and its status
"""
self.logger.info('Stop instance {0}'.format(self.name))
return self.discovery.instances().stop(
project=self.project,
zone=basename(self.zone),
instance=self.name).execute()

@utils.sync_operation
@check_response
def start(self):
"""
Start GCP instance.
Zone operation.
:return: REST response with operation responsible for the instance
Start process and its status
"""
self.logger.info('Start instance {0}'.format(self.name))
return self.discovery.instances().start(
project=self.project,
zone=basename(self.zone),
instance=self.name).execute()

@utils.async_operation(get=True)
@check_response
def create(self):
Expand Down Expand Up @@ -422,23 +473,32 @@ def create(instance_type,

ctx.instance.runtime_properties[constants.RESOURCE_ID] = instance.name
ctx.instance.runtime_properties[constants.NAME] = instance.name
ctx.instance.runtime_properties[constants.MACHINE_TYPE] = \
instance.machine_type
utils.create(instance)


@operation(resumable=True)
@utils.throw_cloudify_exceptions
def start(**kwargs):
def start(name, **kwargs):
ctx.logger.info('Start operation')
gcp_config = utils.get_gcp_config()
props = ctx.instance.runtime_properties
instance = Instance(gcp_config,
ctx.logger,
name=props[constants.NAME],
zone=basename(props['zone']),
)

utils.resource_started(ctx, instance)
if not name:
name = props.get(constants.NAME)

set_ip(instance)
if name:
instance = Instance(gcp_config,
ctx.logger,
name=name,
zone=basename(props['zone']),
)

utils.resource_started(ctx, instance)
set_ip(instance)

instance.start()


@operation(resumable=True)
Expand All @@ -461,6 +521,52 @@ def delete(name, zone, **kwargs):
utils.delete_if_not_external(instance)


@operation(resumable=True)
@utils.throw_cloudify_exceptions
def stop(name, zone, **kwargs):
ctx.logger.info('Stop operation')
gcp_config = utils.get_gcp_config()
props = ctx.instance.runtime_properties

if not zone:
zone = props.get('zone')
if not name:
name = props.get(constants.NAME)

if name:
instance = Instance(gcp_config,
ctx.logger,
name=name,
zone=zone,
)
instance.stop()


@operation(resumable=True)
@utils.throw_cloudify_exceptions
def resize(name, zone, machine_type, **kwargs):
ctx.logger.info('Resize operation')
gcp_config = utils.get_gcp_config()
props = ctx.instance.runtime_properties

if not zone:
zone = props.get('zone')
if not name:
name = props.get(constants.NAME)

if name:
instance = Instance(gcp_config,
ctx.logger,
name=name,
zone=zone,
)
instance.stop()
instance.set_machine_type(name, zone, machine_type)
instance.start()
ctx.instance.runtime_properties[constants.MACHINE_TYPE] = machine_type
instance.machine_type = machine_type


@operation(resumable=True)
@utils.throw_cloudify_exceptions
def add_instance_tag(instance_name, zone, tag, **kwargs):
Expand Down
25 changes: 21 additions & 4 deletions cloudify_gcp/compute/tests/test_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,12 @@ def test_create(self, mock_build, *args):
project='not really a project',
zone='zone'
)

self.assertEqual(
{
'zone': 'zone',
'startup_script': {'type': 'string'},
'name': 'name',
'machine_type': 'instance_type',
'resource_id': 'name',
'_operation': mock_build().instances().insert().execute(),
},
Expand Down Expand Up @@ -172,6 +172,8 @@ def test_create(self, mock_build, *args):
'you pass': 'the test',
'zone': 'zone',
'resource_id': 'name',
'name': 'name',
'machine_type': 'instance_type'
},
self.ctxmock.instance.runtime_properties
)
Expand Down Expand Up @@ -527,12 +529,27 @@ def test_create_with_script_and_windows_agent(self, mock_build, *args):
def test_start(self, mock_getitem, mock_build, *args):
self.ctxmock.node.properties['external_ip'] = False
self.ctxmock.instance.runtime_properties['name'] = 'name'
instance.start()

instance.start('name')
self.assertEqual(
self.ctxmock.instance.runtime_properties['ip'],
'a')

def test_resize(self, mock_build, *args):
instance.resize('foo', 'bar', 'baz')
mock_build().instances().stop.assert_called_with(
project='not really a project',
instance='foo',
zone='bar')
mock_build().instances().setMachineType.assert_called_with(
project='not really a project',
instance='foo',
zone='bar',
body={'machineType': 'bar/machineTypes/baz'})
mock_build().instances().start.assert_called_with(
project='not really a project',
instance='foo',
zone='bar')

@patch('cloudify_gcp.utils.get_item_from_gcp_response', return_value={
'networkInterfaces': [
{
Expand All @@ -543,7 +560,7 @@ def test_start(self, mock_getitem, mock_build, *args):
def test_start_with_external_ip(self, mock_getitem, mock_build, *args):
self.ctxmock.node.properties['external_ip'] = True
self.ctxmock.instance.runtime_properties['name'] = 'name'
instance.start()
instance.start('name')

self.assertEqual(
self.ctxmock.instance.runtime_properties[
Expand Down
1 change: 1 addition & 0 deletions cloudify_gcp/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
ZONE = 'zone'
NETWORK = 'network'
NAME = 'name'
MACHINE_TYPE = 'machine_type'

GCP_OP_DONE = 'DONE'

Expand Down
2 changes: 1 addition & 1 deletion cloudify_gcp/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ def wrapper(self, *args, **kwargs):
raise

if has_finished:
for key in '_operation', 'name', 'selfLink':
for key in '_operation', 'selfLink':
props.pop(key, None)
if get:
props.update(self.get())
Expand Down
20 changes: 19 additions & 1 deletion plugin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins:
gcp_plugin:
executor: central_deployment_agent
package_name: cloudify-gcp-plugin
package_version: '1.8.7'
package_version: '1.8.8'

dsl_definitions:
use_external_resource_desc: &use_external_resource_desc >
Expand Down Expand Up @@ -219,13 +219,31 @@ node_types:
inputs:
name:
default: { get_attribute: [SELF, name]}
stop:
implementation: gcp_plugin.cloudify_gcp.compute.instance.stop
inputs:
name:
default: { get_attribute: [ SELF, name ] }
zone:
default: { get_attribute: [ SELF, zone ] }
delete:
implementation: gcp_plugin.cloudify_gcp.compute.instance.delete
inputs:
name:
default: { get_attribute: [SELF, name] }
zone:
default: { get_attribute: [SELF, zone]}
cloudify.interfaces.operation:
resize:
implementation: gcp_plugin.cloudify_gcp.compute.instance.resize
inputs:
name:
default: { get_attribute: [ SELF, name ] }
zone:
default: { get_attribute: [ SELF, zone ] }
machine_type:
type: string
default: 'e2-standard-2'

cloudify.gcp.nodes.Instance:
derived_from: cloudify.nodes.gcp.Instance
Expand Down
11 changes: 11 additions & 0 deletions plugin_1_4.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,17 @@ node_types:
default: { get_attribute: [SELF, name] }
zone:
default: { get_attribute: [SELF, zone]}
cloudify.interfaces.operation:
resize:
implementation: gcp_plugin.cloudify_gcp.compute.instance.resize
inputs:
name:
default: { get_attribute: [ SELF, name ] }
zone:
default: { get_attribute: [ SELF, zone ] }
machine_type:
type: string
default: 'e2-standard-2'

cloudify.gcp.nodes.Instance:
derived_from: cloudify.nodes.gcp.Instance
Expand Down
11 changes: 11 additions & 0 deletions v2_plugin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,17 @@ node_types:
default: { get_attribute: [SELF, name] }
zone:
default: { get_attribute: [SELF, zone]}
cloudify.interfaces.operation:
resize:
implementation: gcp_plugin.cloudify_gcp.compute.instance.resize
inputs:
name:
default: { get_attribute: [ SELF, name ] }
zone:
default: { get_attribute: [ SELF, zone ] }
machine_type:
type: string
default: 'e2-standard-2'

cloudify.gcp.nodes.Instance:
derived_from: cloudify.nodes.gcp.Instance
Expand Down

0 comments on commit 728047a

Please sign in to comment.