I want to list all my security groups but i want to check if any security group has instances or no, if yes i want to get this instances.
How can i do that in code ? I just build function to get all security group.
#Override
public List<LocalSecurityGroupV2> getSecurityGroups(final List<String> securityGroupIds) {
return securityGroupIds
.stream()
.map(this::findSecurityGroupById)
.collect(Collectors.toList());
}
Can you please elaborate what do you mean by instances?
Is this you are looking for?
Gets all network security groups in a subscription:
https://learn.microsoft.com/en-us/rest/api/virtualnetwork/networksecuritygroups/listall
Gets all network security groups in a resource group:
https://learn.microsoft.com/en-us/rest/api/virtualnetwork/networksecuritygroups/list
We could do that with Azure Java SDK. For authentication please refer to Authenticate with the Azure management libraries for Java.
The following is the demo code, I tested on my side.
String client = "clientId";
String tenant = "tenantId";
String key = "scret key";
ApplicationTokenCredentials credentials = new ApplicationTokenCredentials(client,
tenant,
key,
AzureEnvironment.AZURE);
Azure azure = Azure.configure().authenticate(credentials).withDefaultSubscription();
List <VirtualMachine> vmlist = azure.virtualMachines().list();
List<String> networkInterfaceList = new ArrayList<String>();
PagedList<NetworkSecurityGroup> list = azure.networkSecurityGroups().listByResourceGroup("resourceGroupName");
//add the networkinterface to the list
for (NetworkSecurityGroup nsg :list
) {
for (String networkInterface :nsg.networkInterfaceIds()
) {
networkInterfaceList.add(networkInterface);
}
};
List<VirtualMachine> virtualMachineList = new ArrayList<VirtualMachine>();
for (VirtualMachine vm :vmlist
) {
for (String vmInterface:vm.networkInterfaceIds()
) {
if(networkInterfaceList.contains(vmInterface))
{
if(!virtualMachineList.contains(vm))
{
virtualMachineList.add(vm);
System.out.println(vm.computerName());
}
}
}
}
Related
I can't find the Java API from the Azure SDK to delete a NetworkSecurityRule resource.
The REST API is documented here.
I use this Maven dependency: com.microsoft.azure:azure-mgmt-network:jar:1.31.0
In my code I hold a reference to a NetworkManager instance and I have a collection of NetworkSecurityRule objects.
Does anyone know how to do it?
Thanks,
Chris
According to my test, we can use the following code. For more details, please refer to the docuemnt
1. create a service principal and assign Reader role for the sp.
az login
az account set --subscription "<your subscription id>"
# it will assign Contributor to the sp at subscription level
az ad sp create-for-rbac -n "mysample" --role Contributor
code
public static void main(String[] args){
String clientId = "your sp appId";
String secret = "your sp password";
String domain = "your tenant domain";
ApplicationTokenCredentials credentials = new ApplicationTokenCredentials(clientId, domain, secret,
AzureEnvironment.AZURE);
Azure azure = AzureAzure.configure().withLogLevel(LogLevel.BASIC).authenticate(credentials)
.withDefaultSubscription();
NetworkSecurityGroup group = azure.networkSecurityGroups().getById(
"your nsg resource id");
for(String i : group.securityRules().keySet()){
System.out.println(i);
}
group.update().withoutRule.apply();
group = azure.networkSecurityGroups().getById(
"/subscriptions/e5b0fcfa-e859-43f3-8d84-5e5fe29f4c68/resourceGroups/testgroup/providers/Microsoft.Network/networkSecurityGroups/test0123");
System.out.println(group.Name());
for(String i : group.securityRules().keySet()){
System.out.println(i);
}
}
In Keycloak 8.0.1 we have a Realm with a Group and Subgroups like this:
group -
subgroup1
subgroup2
...
We need to insert a batch of subgroups and users into group. The subgroup should have some attributes.
How can I do this?
I tried:
Using an exported realm-export.json file with newly added subgroups and "Overwrite" on the import. Now I don't see how to connect the new user with the subgroup. And I am also not sure if old users will not be removed this way.
Calling the Keycloak REST API. It doesn't seem possible to UPDATE a group and add subgroups. Documentation says:
PUT /{realm}/groups/{id}Update group, ignores subgroups.
Now I am looking at using a UI testing tool to add the user programmatically, but this seems needlessly complex.
Is it possible to programmatically add new subgroups with users associated to that subgroup? Am I missing something with the REST API call or the import functionality? Is there maybe another way via for example the Java Admin Client?
You can create groups and subgroups under it , Here is the sample code to create subgroups using Admin Client. You can also associate users to those groups
public void addSubgroups() {
RealmResource realm =keycloak.realm("myrealm");
GroupRepresentation topGroup = new GroupRepresentation();
topGroup.setName("group");
topGroup = createGroup(realm, topGroup);
createSubGroup(realm,topGroup.getId(),"subgroup1");
createSubGroup(realm,topGroup.getId(),"subgroup2");
}
private void createSubGroup(RealmResource realm, String parentGroupId, String subGroupName) {
GroupRepresentation subgroup = new GroupRepresentation();
subgroup.setName(subGroupName);
try (Response response = realm.groups().group(parentGroupId).subGroup(subgroup)){
if (response.getStatusInfo().getFamily() == Family.SUCCESSFUL) {
System.out.println("Created Subgroup : " + subGroupName );
} else {
logger.severe("Error Creating Subgroup : " + subGroupName + ", Error Message : " + getErrorMessage(response));
}
}
}
private GroupRepresentation createGroup(RealmResource realm, GroupRepresentation group) {
try (Response response = realm.groups().add(group)) {
String groupId = getCreatedId(response);
group.setId(groupId);
return group;
}
}
getCreatedId(response);
This method in above answer (by ravthiru) is belongs to CreatedResponseUtil from package (org.keycloak.admin.client)
CreatedResponseUtil.getCreatedId(response);
There is an existing subnet say subnet-11223344. In my code I want to know the VPC it belongs to.
I am referring java SDK http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/ec2/model/Subnet.html
// I am not sure if this actually refres to a subnet that I want
Subnet subnet = new Subnet().withSubnetId("subnet-11223344");
String vpcId = subnet.getVpcId();
System.out.println("VPC id"+vpcId);
This returns null.
Here's an example of a general purpose solution provided by ProgramCreek:
public List<Subnet> getSubnets(List<String> subnetIds, AmazonEC2 ec2Client) {
DescribeSubnetsRequest request = new DescribeSubnetsRequest();
if (subnetIds != null && !subnetIds.isEmpty()) {
request = request.withSubnetIds(subnetIds);
}
DescribeSubnetsResult result = ec2Client.describeSubnets(request);
return result.getSubnets();
}
i am trying to create EC2 instances on AWS using the aws sdk for java..
here is the runinstance method
public static RunInstancesResult createInstaince() {
RunInstancesRequest runInstancesRequest = new RunInstancesRequest();
runInstancesRequest.withImageId("ami-ca381398")
.withInstanceType("t2.micro")
.withMinCount(1)
.withMaxCount(1)
.withKeyName("java-sdk")
.withSubnetId("subnet-8eca36f9")
.withSecurityGroupIds("sg-3f00a25a");
RunInstancesResult runInstancesResult = amazonEC2Client
.runInstances(runInstancesRequest);
return runInstancesResult;
}
here i have explicitly specified java-sdk as the key-pair,but now i want the user to be able to choose the key-pair available on the aws server..i saw a function getAMI that gets the AMI from the aws server.can anyone tell me if a function like getKey-pair is possible?
DescribeKeyPairs is what you are looking for. The link is for EC2 API documentation. You may want to look for aws-java sdk method which calls this API.
public static List<String> getKeyName() {
DescribeKeyPairsRequest dkpr = new DescribeKeyPairsRequest();
DescribeKeyPairsResult dkpresult =
amazonEC2Client.describeKeyPairs(dkpr);
List<KeyPairInfo> keyPairs = dkpresult.getKeyPairs();
List<String> keyPairNameList = new ArrayList<String>();
for (KeyPairInfo keyPairInfo : keyPairs) {
keyPairNameList.add(keyPairInfo.getKeyName());
}
for (int i = 0; i < keyPairs.size(); i++) {
System.out.println(keyPairNameList.get(i));
}
return keyPairNameList;
}
this is the code it will return an arraylist of keynames.
I want to get all instance id with a particular tag running under my AWS account using java aws sdk. can someone please guide me how can i get this.thanks
I did it by using filter and for example get all the instances created by same key-pair value
DescribeInstancesRequest request = new DescribeInstancesRequest();
List<String> valuesT1 = new ArrayList<String>();
valuesT1.add("my-keypair-name");
Filter filter = new Filter("key-name", valuesT1);
DescribeInstancesResult result = ec2.describeInstances(request.withFilters(filter));
List<Reservation> reservations = result.getReservations();
for (Reservation reservation : reservations) {
List<Instance> instances = reservation.getInstances();
for (Instance instance : instances) {
System.out.println(instance.getInstanceId());
}
}