Skip to content

Commit

Permalink
Make instance type list calls with region name and account uuid (#209)
Browse files Browse the repository at this point in the history
(cherry picked from commit 3c3c4a5561fdec6a586396ace69347307b3278cd)
  • Loading branch information
GullapalliAkhil authored and abhijeetkaurav1st committed Mar 6, 2023
1 parent 57e67b4 commit d3ca5eb
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 55 deletions.
76 changes: 45 additions & 31 deletions calm/dsl/providers/plugins/aws_vm/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,22 @@ 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"]))

entity_list = []
res = res.json()
for entity in res["entities"]:
entity_list.append(entity["metadata"]["name"])

entity_list.sort()
return entity_list

def volume_types(self):
Expand Down Expand Up @@ -322,19 +327,24 @@ 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"]))

entity_list = []
res = res.json()
for entity in res["entities"]:
entity_list.append(entity["metadata"]["name"])

entity_list.sort()
return entity_list

def volume_types(self):
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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")),
Expand Down
1 change: 0 additions & 1 deletion tests/cli/provider_plugins/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 0 additions & 1 deletion tests/cli/provider_plugins/aws/normal_spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
41 changes: 34 additions & 7 deletions tests/cli/provider_plugins/aws/vm_spec_with_instance_type.yaml
Original file line number Diff line number Diff line change
@@ -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]
6 changes: 3 additions & 3 deletions tests/cli/provider_plugins/aws/vm_spec_with_region.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]
- [5, accounts/primary_account/regions/us-east-1/index]
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down
17 changes: 8 additions & 9 deletions tests/cli/provider_plugins/populators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"] = {}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d3ca5eb

Please sign in to comment.