0

I have an existing MySQL server created on Azure cloud. Now I want to establish a private endpoint connection using Java code (SDK). Can you please help me which Java class to use to achieve this problems. Azure cloud screenshot

User389
  • 15
  • 3
Achintya
  • 77
  • 8
  • AFAIK you don't need any code change. It's only a infrastructure change. – krishg Aug 31 '20 at 11:52
  • I did not get you point, I want to create an API which will create a private endpoint, so for this which java class to be use. – Achintya Sep 01 '20 at 07:24
  • From your question, I thought you meant only this https://learn.microsoft.com/en-us/azure/mysql/concepts-data-access-security-private-link . But now looks like your ask is more around as answered by Jim Xu below? – krishg Sep 01 '20 at 08:43
  • yes Jim answered :) – Achintya Sep 02 '20 at 12:47

1 Answers1

0

If you want to establish a private endpoint with java code, please refer to following code

  1. Create a service principal and assign contributor role to the sp
az login
# it will create a service principal and assign a contributor rolen to the sp
az ad sp create-for-rbac -n "MyApp"   --sdk-auth

enter image description here

  1. sdk
<dependency>
      <groupId>com.microsoft.azure</groupId>
      <artifactId>azure</artifactId>
      <version>1.36.1</version>
    </dependency>
  1. Code
String secret ="<the sp client secret>" ;
        String domain="<the sp tenantId>";
        String clientId="<the sp clientId>";
        String subscriptionId="";
        String groupName="testmysql";
        Region region = Region.JAPAN_EAST;
        String networkName="mysqlvnet1";
        String subnetName="default";
String serverId="<the mysql server resource id e.g. /subscriptions/b83c1ed3-c5b6-44fb-b5ba-2b83a074c23f/resourceGroups/v-wenxu-chinacxp/providers/Microsoft.DBforMySQL/servers/testsql08>"
        ApplicationTokenCredentials credentials = new ApplicationTokenCredentials(clientId,domain,secret, AzureEnvironment.AZURE);

      

        NetworkManager networkManager =NetworkManager.authenticate(credentials,subscriptionId);
        // create vnet
        Network network = networkManager.networks()
                .define(networkName)
                .withRegion(region)
                .withExistingResourceGroup(groupName)
                .withAddressSpace("172.31.0.0/16")
                .defineSubnet(subnetName)
                    .withAddressPrefix("172.31.0.0/24")
                    .attach()
                .create();
        network.subnets().get(subnetName).inner().withPrivateEndpointNetworkPolicies("Disabled");
        network.subnets().get(subnetName).inner().withPrivateLinkServiceNetworkPolicies("Enabled");

        network.update()
                .updateSubnet(subnetName)
                .parent()
                .apply();

        // create network private endpoint.
        PrivateLinkServiceConnection privateLinkServiceConnection = new PrivateLinkServiceConnection()
                .withName("mysql1")
                .withPrivateLinkServiceId("the resource id of mysql service")
                .withPrivateLinkServiceConnectionState(new PrivateLinkServiceConnectionState()
                        .withStatus("Approved"))
                .withGroupIds(Arrays.asList(serverId);
        PrivateEndpointInner privateEndpoint = new PrivateEndpointInner()
                .withPrivateLinkServiceConnections(Arrays.asList(privateLinkServiceConnection))
                .withSubnet(network.subnets().get(subnetName).inner());

        privateEndpoint.withLocation(region.toString());
        privateEndpoint =networkManager.inner().privateEndpoints().createOrUpdate(groupName,"mysql1",privateEndpoint);

enter image description here

Jim Xu
  • 21,610
  • 2
  • 19
  • 39