From d3ca5eb7459233e64a811e325bdc2d74b04ea89a Mon Sep 17 00:00:00 2001 From: Gullapalli Akhil Sai Date: Mon, 6 Feb 2023 14:32:36 +0530 Subject: [PATCH] Make instance type list calls with region name and account uuid (#209) (cherry picked from commit 3c3c4a5561fdec6a586396ace69347307b3278cd) --- calm/dsl/providers/plugins/aws_vm/main.py | 76 +++++++++++-------- tests/cli/provider_plugins/__init__.py | 1 - .../cli/provider_plugins/aws/normal_spec.yaml | 1 - .../aws/vm_spec_with_instance_type.yaml | 41 ++++++++-- .../aws/vm_spec_with_region.yaml | 6 +- .../vm_spec_with_region_dependent_data.yaml | 6 +- tests/cli/provider_plugins/populators.py | 17 ++--- 7 files changed, 93 insertions(+), 55 deletions(-) diff --git a/calm/dsl/providers/plugins/aws_vm/main.py b/calm/dsl/providers/plugins/aws_vm/main.py index fd4f175f..96748cfa 100644 --- a/calm/dsl/providers/plugins/aws_vm/main.py +++ b/calm/dsl/providers/plugins/aws_vm/main.py @@ -122,9 +122,14 @@ def regions(self, account_id): return region_list - def machine_types(self): + def machine_types(self, account_uuid, region_name): + payload = { + "filter": "account_uuid=={};region-name=={}".format( + account_uuid, region_name + ) + } Obj = get_resource_api(self.MACHINE_TYPES, self.connection) - res, err = Obj.list() + res, err = Obj.list(payload) if err: raise Exception("[{}] - {}".format(err["code"], err["error"])) @@ -132,7 +137,7 @@ def machine_types(self): res = res.json() for entity in res["entities"]: entity_list.append(entity["metadata"]["name"]) - + entity_list.sort() return entity_list def volume_types(self): @@ -322,11 +327,16 @@ def regions(self, account_id): return region_list - def machine_types(self): + def machine_types(self, account_uuid, region_name): + payload = { + "filter": "account_uuid=={};region-name=={}".format( + account_uuid, region_name + ) + } Obj = get_resource_api( self.MACHINE_TYPES, self.connection, calm_api=self.calm_api ) - res, err = Obj.list() + res, err = Obj.list(payload) if err: raise Exception("[{}] - {}".format(err["code"], err["error"])) @@ -334,7 +344,7 @@ def machine_types(self): res = res.json() for entity in res["entities"]: entity_list.append(entity["metadata"]["name"]) - + entity_list.sort() return entity_list def volume_types(self): @@ -607,31 +617,6 @@ def create_spec(client): ) ) - choice = click.prompt( - "\n{}(y/n)".format(highlight_text("Want to add an instance type")), default="n" - ) - - ins_types = Obj.machine_types() if choice[0] == "y" else None - - if (not ins_types) and (choice[0] == "y"): - click.echo("\n{}".format(highlight_text("No Instance Profile present"))) - - elif ins_types: - click.echo("\nChoose from given instance types") - for ind, name in enumerate(ins_types): - click.echo("\t {}. {}".format(str(ind + 1), highlight_text(name))) - - while True: - res = click.prompt("\nEnter the index of instance type", default=1) - if (res > len(ins_types)) or (res <= 0): - click.echo("Invalid index !!! ") - - else: - instance_type = ins_types[res - 1] - spec["resources"]["instance_type"] = instance_type - click.echo("{} selected".format(highlight_text(instance_type))) - break - choice = ( click.prompt( "\n{}(y/n)".format(highlight_text("Want to add a region")), default="n" @@ -660,6 +645,35 @@ def create_spec(client): click.echo("{} selected".format(highlight_text(region_name))) break + choice = ( + click.prompt( + "\n{}(y/n)".format(highlight_text("Want to add an instance type")), + default="n", + ) + if region_name + else "n" + ) + + ins_types = Obj.machine_types(account_id, region_name) if choice[0] == "y" else None + if (not ins_types) and (choice[0] == "y"): + click.echo("\n{}".format(highlight_text("No Instance Profile present"))) + + elif ins_types: + click.echo("\nChoose from given instance types") + for ind, name in enumerate(ins_types): + click.echo("\t {}. {}".format(str(ind + 1), highlight_text(name))) + + while True: + res = click.prompt("\nEnter the index of instance type", default=1) + if (res > len(ins_types)) or (res <= 0): + click.echo("Invalid index !!! ") + + else: + instance_type = ins_types[res - 1] + spec["resources"]["instance_type"] = instance_type + click.echo("{} selected".format(highlight_text(instance_type))) + break + choice = ( click.prompt( "\n{}(y/n)".format(highlight_text("Want to add a availability zone")), diff --git a/tests/cli/provider_plugins/__init__.py b/tests/cli/provider_plugins/__init__.py index 52c832e5..df8bb608 100644 --- a/tests/cli/provider_plugins/__init__.py +++ b/tests/cli/provider_plugins/__init__.py @@ -202,7 +202,6 @@ def cls_test_runner(*arg, **kwargs): populator_object = get_resource_populator_object() provider_resource_info = populator_object.get_resource_info(provider_type) resolve_input_dependencies(input, dependencies, provider_resource_info) - run_test( input, cli_assertions, diff --git a/tests/cli/provider_plugins/aws/normal_spec.yaml b/tests/cli/provider_plugins/aws/normal_spec.yaml index dc80b561..9677175e 100644 --- a/tests/cli/provider_plugins/aws/normal_spec.yaml +++ b/tests/cli/provider_plugins/aws/normal_spec.yaml @@ -4,7 +4,6 @@ input: - "1" # account - "vm-@@{calm_array_index}@@-@@{calm_time}@@" # instance name - "y" # Enable Associate Public Ip Address -- "n" # Want Instance type - "n" # Want Region - "n" # Want user-data - "n" # Want tags diff --git a/tests/cli/provider_plugins/aws/vm_spec_with_instance_type.yaml b/tests/cli/provider_plugins/aws/vm_spec_with_instance_type.yaml index 7a1462ef..9753cee7 100644 --- a/tests/cli/provider_plugins/aws/vm_spec_with_instance_type.yaml +++ b/tests/cli/provider_plugins/aws/vm_spec_with_instance_type.yaml @@ -1,26 +1,53 @@ input: -- "1" # project = default +- "1" # project = default - "1" # account - "vm-@@{calm_array_index}@@-@@{calm_time}@@" # instance name - "y" # Enable Associate Public Ip Address +- "y" # Want Region +- "1" # Region = us-east-1 - "y" # Want Instance type -- "61" # Instance Type = t2.nano -- "n" # Want Region -- "n" # Want user-data -- "n" # Want tags +- "61" # Instance Type = t2.nano +- "y" # Want availability zone +- "1" # Availability Zone = us-east-1a +- "y" # Want machine image +- "31" # Machine Image = CentOS Linux 7 x86_64 HVM EBS 1704_01-b7ee8a69-ee97-4a49-9e68-afaee216db2e-ami-d52f5bc3.4 +- "y" # Want IAM Role +- "1" # IAM Role = aws-elasticbeanstalk-ec2-role +- "y" # Want Key Pair +- "6" # Key-Pair = calm-blueprints +- "y" # Want VPC +- "3" # VPC = vpc-ffd54d98 +- "y" # Want Security Group +- "n" # Include Classic Security Group +- "56" # Security Group = sg-184ead62 +- "n" # Want more security Groups +- "y" # Want Subnets +- "1" # Subnet = subnet-c599a5ef +- "y" # Want user-data +- "Sample aws data" # User data +- "y" # Want tags +- "cloud" # Key of tag +- "aws" # Value of tag +- "n" # Want more tags - "8" # Size of root disk - "1" # Volume Type = Provisioned IOPS SSD - "n" # Want to delete on termination +- "y" # Want more disks +- "1" # Device Name = /dev/sdb +- "1" # Volume Type = Provisioned IOPS SSD +- "8" # Size of root disk +- "y" # Want to delete on termination - "n" # Want more disks cli_assertions: - "Create spec for your AWS VM:" spec_assertions: -- t2.nano # Check for instance name +- "t2.nano" # Check for instance name dependencies: # For changing indexes of resource in input as per the setup - [0, projects/default] -- [5, instance_types/t2.nano] +- [5, accounts/primary_account/regions/us-east-1/index] +- [7, accounts/primary_account/regions/us-east-1/instance_types/t2.nano] diff --git a/tests/cli/provider_plugins/aws/vm_spec_with_region.yaml b/tests/cli/provider_plugins/aws/vm_spec_with_region.yaml index f59fe797..67d3e956 100644 --- a/tests/cli/provider_plugins/aws/vm_spec_with_region.yaml +++ b/tests/cli/provider_plugins/aws/vm_spec_with_region.yaml @@ -4,10 +4,10 @@ input: - "1" # account - "vm-@@{calm_array_index}@@-@@{calm_time}@@" # instance name - "y" # Enable Associate Public Ip Address -- "y" # Want Instance type -- "61" # Instance Type = t2.nano - "y" # Want Region - "1" # Region = us-east-1 +- "y" # Want Instance type +- "61" # Instance Type = t2.nano - "n" # Want availability zone - "n" # Want machine image - "n" # Want IAM Role @@ -29,4 +29,4 @@ spec_assertions: dependencies: # For changing indexes of resource in input as per the setup - [0, projects/default] -- [7 , accounts/primary_account/regions/us-east-1/index] \ No newline at end of file +- [5, accounts/primary_account/regions/us-east-1/index] \ No newline at end of file diff --git a/tests/cli/provider_plugins/aws/vm_spec_with_region_dependent_data.yaml b/tests/cli/provider_plugins/aws/vm_spec_with_region_dependent_data.yaml index 32020ac7..c4a22f2b 100644 --- a/tests/cli/provider_plugins/aws/vm_spec_with_region_dependent_data.yaml +++ b/tests/cli/provider_plugins/aws/vm_spec_with_region_dependent_data.yaml @@ -4,10 +4,10 @@ input: - "1" # account - "vm-@@{calm_array_index}@@-@@{calm_time}@@" # instance name - "y" # Enable Associate Public Ip Address -- "y" # Want Instance type -- "61" # Instance Type = t2.nano - "y" # Want Region - "1" # Region = us-east-1 +- "y" # Want Instance type +- "61" # Instance Type = t2.nano - "y" # Want availability zone - "1" # Availability Zone = us-east-1a - "y" # Want machine image @@ -55,7 +55,7 @@ spec_assertions: dependencies: # For changing indexes of resource in input as per the setup - [0 , projects/default] -- [7 , accounts/primary_account/regions/us-east-1/index] +- [5, accounts/primary_account/regions/us-east-1/index] - [9 , accounts/primary_account/regions/us-east-1/availability_zones/us-east-1a] - [11 , accounts/primary_account/regions/us-east-1/machine_images/DND_CENTOS_QA] - [13 , accounts/primary_account/regions/us-east-1/iam_roles/aws-elasticbeanstalk-ec2-role] diff --git a/tests/cli/provider_plugins/populators.py b/tests/cli/provider_plugins/populators.py index 4c0598c6..929a617a 100644 --- a/tests/cli/provider_plugins/populators.py +++ b/tests/cli/provider_plugins/populators.py @@ -80,15 +80,6 @@ def __populate_aws_resources(self): "uuid": entity_id } - # get instance_types - instance_types = Obj.machine_types() - self.aws_resource_info["instance_types"] = {} - for type in CONSTANTS.AWS.INSTANCE_TYPES: - if type in instance_types: - self.aws_resource_info["instance_types"][type] = instance_types.index( - type - ) - # get volume_types volume_types_names = list(AWS_CONSTANTS.VOLUME_TYPE_MAP.keys()) self.aws_resource_info["volume_types"] = {} @@ -187,6 +178,14 @@ def __populate_aws_resources(self): } } + # get instance_types + instance_types = Obj.machine_types(account_uuid, region) + region_related_info["instance_types"] = {} + for instance in CONSTANTS.AWS.INSTANCE_TYPES: + if instance in instance_types: + region_related_info["instance_types"][ + instance + ] = instance_types.index(instance) self.aws_resource_info["accounts"][account]["regions"][ region ] = region_related_info