Skip to content

Commit bdf37d0

Browse files
authored
Merge pull request #49 from unterhol/master
add security group examples
2 parents d5c19da + 2e8195c commit bdf37d0

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.softlayer.api.example;
2+
3+
import com.softlayer.api.ApiClient;
4+
import com.softlayer.api.service.network.SecurityGroup;
5+
import com.softlayer.api.service.network.securitygroup.Rule;
6+
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
10+
/** Create a simple security group */
11+
public class AddSecurityGroupRule extends Example {
12+
13+
@Override
14+
public void run(ApiClient client) throws Exception {
15+
SecurityGroup.Service service = SecurityGroup.service(client);
16+
17+
// create a new security group
18+
SecurityGroup sg = new SecurityGroup();
19+
sg.setName("javaTest");
20+
sg.setDescription("javaTestDescription");
21+
22+
// create that security group
23+
SecurityGroup sgOut = service.createObject(sg);
24+
System.out.format("Created security group with ID: %s\n", sgOut.getId());
25+
26+
// bind the service to the id of the newly created security group
27+
service = sgOut.asService(client);
28+
29+
// Create a security group rule
30+
Rule rule = new Rule();
31+
rule.setDirection("ingress");
32+
rule.setProtocol("udp");
33+
34+
List<Rule> newRules = new ArrayList<Rule>();
35+
newRules.add(rule);
36+
37+
// Now add the rule(s) to the security group
38+
System.out.println("Adding rule(s) to security group");
39+
service.addRules(newRules);
40+
}
41+
42+
public static void main(String[] args) throws Exception {
43+
new AddSecurityGroupRule().start(args);
44+
}
45+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.softlayer.api.example;
2+
3+
import com.softlayer.api.ApiClient;
4+
import com.softlayer.api.service.network.SecurityGroup;
5+
6+
/** Create security group example. */
7+
public class CreateSecurityGroup extends Example {
8+
9+
@Override
10+
public void run(ApiClient client) throws Exception {
11+
SecurityGroup.Service service = SecurityGroup.service(client);
12+
13+
// Create a java object representing the new security group
14+
SecurityGroup sg = new SecurityGroup();
15+
sg.setName("javaTest");
16+
sg.setDescription("javaTestDescription");
17+
18+
// Now call the security group service to create it
19+
System.out.println("Make call to create security group");
20+
SecurityGroup sgOut = service.createObject(sg);
21+
System.out.format("Created security group with name = %s\n", sgOut.getName());
22+
}
23+
24+
public static void main(String[] args) throws Exception {
25+
new CreateSecurityGroup().start(args);
26+
}
27+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.softlayer.api.example;
2+
3+
import com.softlayer.api.ApiClient;
4+
import com.softlayer.api.service.Account;
5+
import com.softlayer.api.service.network.SecurityGroup;
6+
7+
/** List all security groups for an account */
8+
public class ListSecurityGroups extends Example {
9+
10+
@Override
11+
public void run(ApiClient client) throws Exception {
12+
// Get the Account service
13+
Account.Service service = Account.service(client);
14+
15+
// To get specific information on an account (security groups in this case) a mask is provided
16+
service.withMask().securityGroups();
17+
18+
// Calling getObject will now use the mask
19+
Account account = service.getObject();
20+
21+
System.out.format("\nFound %d security groups\n", account.getSecurityGroups().size());
22+
23+
for (SecurityGroup sg : account.getSecurityGroups()) {
24+
System.out.format("id: %s name: %s \n", sg.getId(), sg.getName());
25+
}
26+
}
27+
28+
public static void main(String[] args) throws Exception {
29+
new ListSecurityGroups().start(args);
30+
}
31+
}

0 commit comments

Comments
 (0)