· 7 years ago · May 29, 2018, 08:46 AM
1package com.radware.vdirect
2
3import com.amazonaws.auth.AWSCredentials
4import com.amazonaws.auth.AWSCredentialsProvider
5import com.amazonaws.services.ec2.AmazonEC2
6import com.amazonaws.services.ec2.AmazonEC2ClientBuilder
7import com.amazonaws.services.ec2.model.DescribeInstancesResult
8import com.radware.alteon.workflow.impl.java.Action
9import com.radware.alteon.workflow.impl.java.Workflow
10import com.radware.vdirect.credentials.CredentialsEntry
11import com.radware.vdirect.credentials.CredentialsTarget
12import com.radware.vdirect.credentials.ICredentialsAccess
13import com.radware.vdirect.credentials.UsernamePassword
14import com.radware.vdirect.server.VDirectServerClient
15import groovy.text.GStringTemplateEngine
16import org.slf4j.Logger
17import org.springframework.beans.factory.annotation.Autowired
18
19@Workflow
20class EC2DemoWorkflow {
21 @Autowired
22 VDirectServerClient vDirectServerClient
23 @Autowired
24 Logger log
25
26 @Action(resultType = "text/html")
27 String connectToEC2() {
28 log.info('Hello vDirect/EC2 Groovy workflow')
29 AmazonEC2 ec2 = AmazonEC2ClientBuilder.standard().withCredentials(new vDirectAWSCredentialsProvider(vDirectServerClient)).withRegion("eu-west-2").build()
30
31 // Lets do something with our ec2 client
32
33 DescribeInstancesResult instances = ec2.describeInstances()
34 List<Map> data = []
35 instances.reservations.each { res ->
36 res.instances.each {inst ->
37
38 data.add(['id':inst.instanceId,
39 'imageId':inst.imageId,
40 'state':inst.state.toString(),
41 'arch':inst.architecture,
42 'ip':inst.publicIpAddress])
43 }
44 }
45 final GStringTemplateEngine engine = new GStringTemplateEngine()
46 engine.createTemplate(TEMPLATE).make(['instances':data]).toString()
47 }
48
49 static class vDirectAWSCredentialsProvider implements AWSCredentialsProvider,AWSCredentials {
50
51 vDirectAWSCredentialsProvider(VDirectServerClient vDirectServerClient) {
52 Optional<CredentialsEntry> credentialsEntryOptional = vDirectServerClient.getCredentialsManager().get(new CredentialsTarget("ec2","ec2","ec2"))
53 if (credentialsEntryOptional.isPresent()) {
54 ICredentialsAccess credentialsManager = vDirectServerClient.getCredentialsAccess()
55 Optional<UsernamePassword> usernamePasswordOptional = credentialsManager.getCredentials(credentialsEntryOptional.get().getTarget(), UsernamePassword)
56 if (usernamePasswordOptional.isPresent()) {
57 UsernamePassword usernamePassword = usernamePasswordOptional.get()
58 this.accessKeyId = usernamePassword.userName
59 this.secretKey = usernamePassword.password
60 } else {
61 throw new RuntimeException("Could not find user/pwd for EC2")
62 }
63 } else {
64 throw new RuntimeException("Could not find CredentialsEntry for EC2")
65 }
66
67 }
68 AWSCredentials getCredentials() {
69 return this
70 }
71 void refresh() {
72 // no op
73 }
74
75 String getAWSAccessKeyId() {
76 return accessKeyId
77 }
78 String getAWSSecretKey() {
79 return secretKey
80 }
81 private String accessKeyId
82 private String secretKey
83 }
84
85 def TEMPLATE = '''
86 <table>
87 <tr>
88 <th>Id</th>
89 <th>ImageId</th>
90 <th>State</th>
91 <th>Arch</th>
92 <th>IP</th>
93 </tr>
94 <% for (instance in instances) { %>
95 <tr>
96 <td>${instance.id}</td>
97 <td>${instance.imageId}</td>
98 <td>${instance.state}</td>
99 <td>${instance.arch}</td>
100 <td>${instance.ip}</td>
101 </tr>
102 <% } %>
103 </table>
104 '''
105}