Created
September 26, 2021 18:12
-
-
Save dmikusa/0f24d4787fa1c6abe84a530e80167cd1 to your computer and use it in GitHub Desktop.
Example of loading security groups & security group guid
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.demo; | |
import org.cloudfoundry.client.CloudFoundryClient; | |
import org.cloudfoundry.client.v2.securitygroups.ListSecurityGroupsRequest; | |
import org.cloudfoundry.client.v2.securitygroups.ListSecurityGroupsResponse; | |
import org.cloudfoundry.client.v2.securitygroups.SecurityGroupResource; | |
import org.cloudfoundry.reactor.ConnectionContext; | |
import org.cloudfoundry.reactor.DefaultConnectionContext; | |
import org.cloudfoundry.reactor.TokenProvider; | |
import org.cloudfoundry.reactor.client.ReactorCloudFoundryClient; | |
import org.cloudfoundry.reactor.doppler.ReactorDopplerClient; | |
import org.cloudfoundry.reactor.tokenprovider.RefreshTokenGrantTokenProvider; | |
import org.cloudfoundry.reactor.uaa.ReactorUaaClient; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.beans.factory.annotation.Value; | |
import org.springframework.boot.CommandLineRunner; | |
import org.springframework.boot.SpringApplication; | |
import org.springframework.boot.autoconfigure.SpringBootApplication; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.stereotype.Component; | |
@SpringBootApplication | |
public class DemoApplication { | |
public static void main(String[] args) { | |
SpringApplication.run(DemoApplication.class, args); | |
} | |
@Component | |
public static class DoStuff implements CommandLineRunner { | |
@Autowired | |
private CloudFoundryClient client; | |
@Override | |
public void run(String... args) throws Exception { | |
client.securityGroups().list( | |
ListSecurityGroupsRequest.builder() | |
.name("default_security_group") // security group name you're looking for or omit and it'll return all | |
.build()) | |
.doOnEach(signal -> { | |
// I'm just printing the name & guid so you can see how to access them from the list response | |
ListSecurityGroupsResponse resp = signal.get(); | |
if (resp != null) { | |
for (SecurityGroupResource resource : resp.getResources()) { | |
System.out.println("Resource: " + resource.getMetadata().getId() + " -- " + resource.getEntity().getName()); | |
} | |
} | |
}) | |
.subscribe(); | |
System.in.read(); | |
} | |
} | |
@Bean | |
DefaultConnectionContext connectionContext(@Value("${cf.apiHost}") String apiHost) { | |
return DefaultConnectionContext.builder() | |
.apiHost(apiHost) | |
.build(); | |
} | |
@Bean | |
RefreshTokenGrantTokenProvider tokenProvider(@Value("${cf.refreshToken}") String refreshToken) { | |
return RefreshTokenGrantTokenProvider.builder() | |
.clientId("cf") | |
.clientSecret("") | |
.token(refreshToken) | |
.build(); | |
} | |
// Or use a password token grant | |
// @Bean | |
// PasswordGrantTokenProvider tokenProvider(@Value("${cf.username}") String username, | |
// @Value("${cf.password}") String password) { | |
// return PasswordGrantTokenProvider.builder() | |
// .password(password) | |
// .username(username) | |
// .build(); | |
// } | |
@Bean | |
ReactorCloudFoundryClient cloudFoundryClient(ConnectionContext connectionContext, TokenProvider tokenProvider) { | |
return ReactorCloudFoundryClient.builder() | |
.connectionContext(connectionContext) | |
.tokenProvider(tokenProvider) | |
.build(); | |
} | |
@Bean | |
ReactorDopplerClient dopplerClient(ConnectionContext connectionContext, TokenProvider tokenProvider) { | |
return ReactorDopplerClient.builder() | |
.connectionContext(connectionContext) | |
.tokenProvider(tokenProvider) | |
.build(); | |
} | |
@Bean | |
ReactorUaaClient uaaClient(ConnectionContext connectionContext, TokenProvider tokenProvider) { | |
return ReactorUaaClient.builder() | |
.connectionContext(connectionContext) | |
.tokenProvider(tokenProvider) | |
.build(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment