update from RIFT as of 696b75d2fe9fb046261b08c616f1bcf6c0b54a9b second try
[osm/SO.git] / rwcal / test / test_aws_image_get.py
1 #!/usr/bin/python3
2
3 #
4 # Copyright 2016 RIFT.IO Inc
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18
19
20 import sys
21 import rw_peas
22 from gi import require_version
23 require_version('RwCal', '1.0')
24
25 from gi.repository import RwcalYang
26 from gi.repository.RwTypes import RwStatus
27 import argparse
28 import logging
29 import rwlogger
30
31 persistent_resources = {
32 'vms' : [],
33 'networks' : [],
34 }
35
36
37 RIFT_IMAGE_AMI = 'ami-7070231a'
38
39 logging.basicConfig(level=logging.ERROR)
40 logger = logging.getLogger('rift.cal.awsresources')
41 logger.setLevel(logging.INFO)
42
43 def get_cal_plugin():
44 """
45 Load AWS cal plugin
46 """
47 plugin = rw_peas.PeasPlugin('rwcal_aws', 'RwCal-1.0')
48 engine, info, extension = plugin()
49 cal = plugin.get_interface("Cloud")
50 rwloggerctx = rwlogger.RwLog.Ctx.new("Cal-Log")
51 try:
52 rc = cal.init(rwloggerctx)
53 assert rc == RwStatus.SUCCESS
54 except Exception as e:
55 logger.error("ERROR:Cal plugin instantiation failed with exception %s", repr(e))
56 else:
57 logger.info("AWS Cal plugin successfully instantiated")
58 return cal
59
60 def get_cal_account(**kwargs):
61 """
62 Returns AWS cal account
63 """
64 account = RwcalYang.YangData_RwProject_Project_CloudAccounts_CloudAccountList()
65 account.account_type = "aws"
66 account.aws.key = kwargs['key']
67 account.aws.secret = kwargs['secret']
68 account.aws.region = kwargs['region']
69 if 'ssh_key' in kwargs and kwargs['ssh_key'] is not None:
70 account.aws.ssh_key = kwargs['ssh_key']
71 account.aws.availability_zone = kwargs['availability_zone']
72 if 'vpcid' in kwargs and kwargs['vpcid'] is not None:
73 account.aws.vpcid = kwargs['vpcid']
74 if 'default_subnet_id' in kwargs and kwargs['default_subnet_id'] is not None:
75 account.aws.default_subnet_id = kwargs['default_subnet_id']
76 return account
77
78 class AWSResources(object):
79 """
80 Class with methods to manage AWS resources
81 """
82 def __init__(self, **kwargs):
83 self._cal = get_cal_plugin()
84 self._acct = get_cal_account(**kwargs)
85
86 def get_image_list(self):
87 """
88 Get Image list
89 """
90 logger.info("Initiating Get image list")
91 rc, rsp = self._cal.get_image_list(self._acct)
92
93 print("Return resp: rsp ", rsp)
94
95 logger.info("Get image list complete")
96
97
98 def get_image(self, image_ami_id):
99 """
100 Get Image from AMI id
101 """
102 logger.info("Initiating Get image")
103 rc, rsp = self._cal.get_image(self._acct, image_ami_id)
104
105 print("Return code: rc ", rc)
106 print("Return resp: rsp ", rsp)
107
108 logger.info("Get image complete")
109
110
111
112 def main():
113 """
114 Main routine
115
116 New AWS credentials were created as follows:
117 User: aws_riftio
118 Access Key ID: AKIAJQ4D3X5WO3P6JXKA
119 Secret Access key: 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
120 Pasword: RhN*q2ze*fpY
121
122 The following AWS cloud account config can be used on LP CLI:
123 cloud account AWS account-type aws aws key AKIAJQ4D3X5WO3P6JXKA secret 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 region us-east-1 vpcid vpc-cb1cd2af ssh-key rift-awskey availability-zone us-east-1c default-subnet-id subnet-73796d04 plugin-name rwcal_aws dynamic-flavor-support true`
124 """
125 parser = argparse.ArgumentParser(description='Script to manage AWS resources')
126
127 parser.add_argument('--aws-key',
128 action = 'store',
129 dest = 'aws_key',
130 type = str,
131 help='AWS key')
132
133 parser.add_argument('--aws-secret',
134 action = 'store',
135 dest = 'aws_secret',
136 type = str,
137 help='AWS secret')
138
139 parser.add_argument('--aws-region',
140 action = 'store',
141 dest = 'aws_region',
142 type = str,
143 help='AWS region')
144
145 parser.add_argument('--aws-az',
146 action = 'store',
147 dest = 'aws_az',
148 type = str,
149 help='AWS Availability zone')
150
151 parser.add_argument('--aws-sshkey',
152 action = 'store',
153 dest = 'aws_sshkey',
154 type = str,
155 help='AWS SSH Key to login to instance')
156
157 parser.add_argument('--aws-vpcid',
158 action = 'store',
159 dest = 'aws_vpcid',
160 type = str,
161 help='AWS VPC ID to use to indicate non default VPC')
162
163 parser.add_argument('--aws-default-subnet',
164 action = 'store',
165 dest = 'aws_default_subnet',
166 type = str,
167 help='AWS Default subnet id in VPC to be used for mgmt network')
168
169 argument = parser.parse_args()
170
171 '''
172 User: aws_riftio
173 Access Key ID: AKIAJQ4D3X5WO3P6JXKA
174 Secret Access key: 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
175 Pasword: RhN*q2ze*fpY
176
177 cloud account AWS account-type aws aws key AKIAJQ4D3X5WO3P6JXKA secret 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 region us-east-1 vpcid vpc-cb1cd2af ssh-key rift-awskey availability-zone us-east-1c default-subnet-id subnet-73796d04 plugin-name rwcal_aws dynamic-flavor-support true
178 '''
179
180 argument.aws_key = "AKIAJQ4D3X5WO3P6JXKA"
181 argument.aws_secret = "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"
182 argument.aws_region = "us-east-1"
183 argument.aws_az = "us-east-1c"
184 argument.aws_sshkey = "rift-awskey"
185 argument.aws_vpcid = "vpc-cb1cd2af"
186 argument.aws_default_subnet = "subnet-73796d04"
187
188 if (argument.aws_key is None or argument.aws_secret is None or argument.aws_region is None or
189 argument.aws_az is None):
190 logger.error("Missing mandatory params. AWS Key, Secret, Region, AZ and SSH key are mandatory params")
191 sys.exit(-1)
192
193
194 ### Start processing
195 logger.info("Instantiating cloud-abstraction-layer")
196 drv = AWSResources(key=argument.aws_key, secret=argument.aws_secret, region=argument.aws_region, availability_zone = argument.aws_az,
197 ssh_key = argument.aws_sshkey, vpcid = argument.aws_vpcid, default_subnet_id = argument.aws_default_subnet)
198 logger.info("Instantiating cloud-abstraction-layer.......[Done]")
199
200 logger.info("Testing image list APIs")
201 drv.get_image_list()
202 logger.info("Finished testing image list APIs")
203
204 logger.info("Testing get image APIs for rift ping image - Present in Owner account")
205 drv.get_image('ami-eb0a5f81')
206 logger.info("Finished testing get image APIs")
207
208 logger.info("Testing get image APIs for public vyos image")
209 drv.get_image('ami-9ea315f6')
210 logger.info("Finished testing get image APIs")
211
212 logger.info("Testing get image APIs for public PalotAlto FW image")
213 drv.get_image('ami-34ca984f')
214 logger.info("Finished testing get image APIs")
215
216
217 if __name__ == '__main__':
218 main()