4 # Copyright 2016 RIFT.IO Inc
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
10 # http://www.apache.org/licenses/LICENSE-2.0
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.
22 from gi
import require_version
23 require_version('RwCal', '1.0')
25 from gi
.repository
import RwcalYang
26 from gi
.repository
.RwTypes
import RwStatus
31 persistent_resources
= {
37 RIFT_IMAGE_AMI
= 'ami-7070231a'
39 logging
.basicConfig(level
=logging
.ERROR
)
40 logger
= logging
.getLogger('rift.cal.awsresources')
41 logger
.setLevel(logging
.INFO
)
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")
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
))
57 logger
.info("AWS Cal plugin successfully instantiated")
60 def get_cal_account(**kwargs
):
62 Returns AWS cal account
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']
78 class AWSResources(object):
80 Class with methods to manage AWS resources
82 def __init__(self
, **kwargs
):
83 self
._cal
= get_cal_plugin()
84 self
._acct
= get_cal_account(**kwargs
)
86 def get_image_list(self
):
90 logger
.info("Initiating Get image list")
91 rc
, rsp
= self
._cal
.get_image_list(self
._acct
)
93 print("Return resp: rsp ", rsp
)
95 logger
.info("Get image list complete")
98 def get_image(self
, image_ami_id
):
100 Get Image from AMI id
102 logger
.info("Initiating Get image")
103 rc
, rsp
= self
._cal
.get_image(self
._acct
, image_ami_id
)
105 print("Return code: rc ", rc
)
106 print("Return resp: rsp ", rsp
)
108 logger
.info("Get image complete")
116 New AWS credentials were created as follows:
118 Access Key ID: AKIAJQ4D3X5WO3P6JXKA
119 Secret Access key: 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
120 Pasword: RhN*q2ze*fpY
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`
125 parser
= argparse
.ArgumentParser(description
='Script to manage AWS resources')
127 parser
.add_argument('--aws-key',
133 parser
.add_argument('--aws-secret',
139 parser
.add_argument('--aws-region',
145 parser
.add_argument('--aws-az',
149 help='AWS Availability zone')
151 parser
.add_argument('--aws-sshkey',
155 help='AWS SSH Key to login to instance')
157 parser
.add_argument('--aws-vpcid',
161 help='AWS VPC ID to use to indicate non default VPC')
163 parser
.add_argument('--aws-default-subnet',
165 dest
= 'aws_default_subnet',
167 help='AWS Default subnet id in VPC to be used for mgmt network')
169 argument
= parser
.parse_args()
173 Access Key ID: AKIAJQ4D3X5WO3P6JXKA
174 Secret Access key: 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
175 Pasword: RhN*q2ze*fpY
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
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"
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")
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]")
200 logger
.info("Testing image list APIs")
202 logger
.info("Finished testing image list APIs")
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")
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")
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")
217 if __name__
== '__main__':