azureazure-sdkazure-mysql-database

Create a private endpoint for a specific MySQL server in Azure


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


Solution

  • 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