Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hmmorales/update sample sdk code for 2021-11-01-preview #23

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions go/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,21 @@ Sample code for using [iotcentral](https://github.com/Azure/azure-sdk-for-go/rel
To begin, simply clone this repository onto your local machine and run the following to install all the necessary dependencies.

```
go mod init main
go get -u github.com/dimchansky/utfbom
go get -u github.com/mitchellh/go-homedir
go get -u golang.org/x/crypto/pkcs12
go get -u github.com/Azure/azure-sdk-for-go
go get -u github.com/Azure/azure-sdk-for-go/services/iotcentral/mgmt/2021-06-01/iotcentral
go get -u github.com/Azure/azure-sdk-for-go/services/preview/iotcentral/mgmt/2021-11-01-preview/iotcentral
go get github.com/Azure/go-autorest/autorest/azure/auth
go build
```

### Usage
Make sure you head over to the main.go file to change the configuration to the one that is shown on your [Microsoft Azure Portal](https://portal.azure.com).

```
go run .\main.go
go run ./main.go
```

Ever wonder what Azure SDK for Go provide in terms of iotcentral? Check [this](https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/services/iotcentral/mgmt/2021-06-01/iotcentral) out.
69 changes: 60 additions & 9 deletions go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ import (
"fmt"
"os"

"github.com/Azure/azure-sdk-for-go/services/iotcentral/mgmt/2021-06-01/iotcentral"
"github.com/Azure/azure-sdk-for-go/services/preview/iotcentral/mgmt/2021-11-01-preview/iotcentral"
"github.com/Azure/go-autorest/autorest/azure/auth"
)

func main() {
subscriptionID := "add-subscription-id-here"
ioTCentralClient := iotcentral.NewAppsClient(subscriptionID)
operationsClient := iotcentral.NewOperationsClient(subscriptionID)
privateEndpointClient := iotcentral.NewPrivateEndpointConnectionsClient(subscriptionID)
privateLinksClient := iotcentral.NewPrivateLinksClient(subscriptionID)

// Before you begin, please make sure to register an app with Azure Active Directory first.
// Follow this article, https://docs.microsoft.com/powerapps/developer/common-data-service/walkthrough-register-app-azure-active-directory
Expand All @@ -24,8 +26,9 @@ func main() {
// You also need to set the Redirect URIs, otherwise, it won't work.
// Check out this article in case you are interested in other ways to authericate https://docs.microsoft.com/azure/go/azure-sdk-go-authorization
// sample code for Authentication with Azure, check out this readme, https://github.com/Azure/azure-sdk-for-go#authentication

applicationID := "add-app-id-here" // client id
directoryID := "add-directory-id-here" // tenant id
directoryID := "add-directory-id-here" // tenant id
deviceConfig := auth.NewDeviceFlowConfig(applicationID, directoryID)
authorizer, authorizerErr := deviceConfig.Authorizer()
if authorizerErr != nil {
Expand All @@ -35,6 +38,8 @@ func main() {
} else {
ioTCentralClient.Authorizer = authorizer
operationsClient.Authorizer = authorizer
privateEndpointClient.Authorizer = authorizer
privateLinksClient.Authorizer = authorizer
}

resourceDisplayName := "resource-display-name"
Expand Down Expand Up @@ -118,9 +123,29 @@ func main() {
}

updatedResourceDisplayName := resourceDisplayName + "-new-name"
publicNetworkAccessEnabled := iotcentral.PossiblePublicNetworkAccessValues()[1]
networkActionAllow := iotcentral.PossibleNetworkActionValues()[0]
filterName := "Localhost"
ipMask := "127.0.0.1"
ipRulesArray := [] iotcentral.NetworkRuleSetIPRule{
{
FilterName: &filterName,
IPMask: &ipMask,
},
}
applyToDevices := true
applyToIOTCentral := false
updateNetworkRuleSets := iotcentral.NetworkRuleSets{
ApplyToDevices: &applyToDevices,
ApplyToIoTCentral: &applyToIOTCentral,
DefaultAction: networkActionAllow,
IPRules: &ipRulesArray,
}
updateAppProperties := iotcentral.AppProperties{
DisplayName: &updatedResourceDisplayName,
Subdomain: &resourceDomainName,
PublicNetworkAccess: publicNetworkAccessEnabled,
NetworkRuleSets: &updateNetworkRuleSets,
}
appPatch := iotcentral.AppPatch{
AppProperties: &updateAppProperties,
Expand Down Expand Up @@ -154,7 +179,7 @@ func main() {
fmt.Println(operationsErr)
os.Exit(1)
} else {
fmt.Print("Here are all the supported operations in the iotc,\n")
fmt.Println("Here are all the supported operations in the iotc,")
operations := operationsResult.Values()
for i := range operations {
fmt.Printf("%v. %v\n", i, *operations[i].Display.Operation)
Expand All @@ -167,19 +192,45 @@ func main() {
fmt.Println(appTemplatesErr)
os.Exit(1)
} else {
fmt.Print("Here are all the iotc app templates,\n")
fmt.Println("Here are all the iotc app templates,")
appTemplates := appTemplatesResult.Values()
for i := range appTemplates {
fmt.Printf("%v. %v\n", i, *appTemplates[i].Name)
}
}

// delete app
deleteAppResult, deleteAppErr := ioTCentralClient.Delete(context.Background(), resourceGroup, resourceDomainName)
if deleteAppErr != nil {
fmt.Println(deleteAppErr)
// list all private endpoint connections
privateEndpointResult, privateEndpointErr := privateEndpointClient.List(context.Background(), resourceGroup, resourceDomainName)
if privateEndpointErr != nil {
fmt.Println(privateEndpointErr)
os.Exit(1)
} else {
fmt.Println(deleteAppResult.Status() + " to delete app")
fmt.Println("Here are all the private endpoint connections in your app,")
privateEndpoint := *privateEndpointResult.Value
for i := range privateEndpoint {
fmt.Printf("%v. %v. %v\n", i, *privateEndpoint[i].Name, *privateEndpoint[i].ID)
}
}

// list all private links
privateLinksResult, privateLinksErr := privateLinksClient.List(context.Background(), resourceGroup, resourceDomainName)
if privateLinksErr != nil {
fmt.Println(privateLinksErr)
os.Exit(1)
} else {
fmt.Println("Here are all the private links in your app,")
privateLinks := *privateLinksResult.Value
for i := range privateLinks {
fmt.Printf("%v. %v. %v\n", i, *privateLinks[i].Name, *privateLinks[i].ID)
}
}

// delete app
// deleteAppResult, deleteAppErr := ioTCentralClient.Delete(context.Background(), resourceGroup, resourceDomainName)
// if deleteAppErr != nil {
// fmt.Println(deleteAppErr)
// os.Exit(1)
// } else {
// fmt.Println(deleteAppResult.Status() + " to delete app")
// }
}
9 changes: 5 additions & 4 deletions java/README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,22 @@ Sample code for using Azure IoT Central Management Plane APIs.

### SDK version

The support SDK version is [1.0.0-beta.2](https://github.com/Azure/azure-sdk-for-java/tree/azure-resourcemanager-iotcentral_1.0.0-beta.2/sdk/iotcentral/azure-resourcemanager-iotcentral).
The support SDK version is [1.1.0-beta.1](https://github.com/Azure/azure-sdk-for-java/tree/azure-resourcemanager-iotcentral_1.1.0-beta.1/sdk/iotcentral/azure-resourcemanager-iotcentral).

See more samples on using IoT Central APIs [here](https://github.com/Azure/azure-sdk-for-java/blob/azure-resourcemanager-iotcentral_1.0.0-beta.2/sdk/iotcentral/azure-resourcemanager-iotcentral/SAMPLE.md).
See more samples on using IoT Central APIs [here](https://github.com/Azure/azure-sdk-for-java/blob/azure-resourcemanager-iotcentral_1.1.0-beta.1/sdk/iotcentral/azure-resourcemanager-iotcentral/SAMPLE.md).

### Usage
To begin, clone this repository, update the authentication settings in IotCentralMgmtApiSampleBase.java, the app configurations in Main.java, and compile/execute the project.

Run the below command in the directory where pom.xml is in

```
cd src
mvn clean compile exec:java
```

See SampleOutput.txt in the basedir for output from the project execution.

Continue developing using the samples shown [here](https://github.com/Azure/azure-sdk-for-java/blob/azure-resourcemanager-iotcentral_1.0.0-beta.2/sdk/iotcentral/azure-resourcemanager-iotcentral/SAMPLE.md).
Continue developing using the samples shown [here](https://github.com/Azure/azure-sdk-for-java/blob/azure-resourcemanager-iotcentral_1.1.0-beta.1/sdk/iotcentral/azure-resourcemanager-iotcentral/SAMPLE.md).

### Additional References:
Overview: Use the Azure SDK for Java | Microsoft Docs
Expand Down
11 changes: 6 additions & 5 deletions java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,31 +46,32 @@
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-core-http-netty</artifactId>
<version>1.11.1</version>
<version>1.11.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.azure.resourcemanager/azure-resourcemanager -->
<dependency>
<groupId>com.azure.resourcemanager</groupId>
<artifactId>azure-resourcemanager</artifactId>
<version>2.9.0</version>
<version>2.14.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.azure/azure-core -->
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-core</artifactId>
<version>1.21.0</version>
<version>1.27.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.azure/azure-identity -->
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.4.0</version>
<version>1.5.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.azure.resourcemanager/azure-resourcemanager-iotcentral -->
<dependency>
<groupId>com.azure.resourcemanager</groupId>
<artifactId>azure-resourcemanager-iotcentral</artifactId>
<version>1.0.0-beta.2</version>
<version>1.1.0-beta.1</version>
</dependency>
</dependencies>
</project>
75 changes: 68 additions & 7 deletions java/src/main/IotCentralMgmtApiSample.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import java.io.IOException;
import java.util.Arrays;

import com.azure.core.http.rest.PagedIterable;
import com.azure.core.management.exception.ManagementException;
import com.azure.core.util.Context;
Expand All @@ -7,12 +9,18 @@
import com.azure.resourcemanager.iotcentral.models.AppSku;
import com.azure.resourcemanager.iotcentral.models.AppSkuInfo;
import com.azure.resourcemanager.iotcentral.models.AppTemplate;
import com.azure.resourcemanager.iotcentral.models.NetworkAction;
import com.azure.resourcemanager.iotcentral.models.NetworkRuleSetIpRule;
import com.azure.resourcemanager.iotcentral.models.NetworkRuleSets;
import com.azure.resourcemanager.iotcentral.models.Operation;
import com.azure.resourcemanager.iotcentral.models.PublicNetworkAccess;
import com.azure.resourcemanager.iotcentral.models.SystemAssignedServiceIdentity;
import com.azure.resourcemanager.iotcentral.models.SystemAssignedServiceIdentityType;
import com.azure.resourcemanager.iotcentral.models.PrivateEndpointConnection;
import com.azure.resourcemanager.iotcentral.models.PrivateLinkResource;

public class IotCentralMgmtApiSample extends IotCentralMgmtApiSampleBase {
private static String defaultLocation = "eastus";
private static String defaultLocation = "eastus2";

public IotCentralMgmtApiSample() throws IOException {
super();
Expand All @@ -38,7 +46,6 @@ public static void appsListTemplates(IotCentralManager manager) {
PagedIterable < AppTemplate > templates = manager.apps().listTemplates(Context.NONE);

printHeader("IOT Central App Templates:");
// System.out.printf("Name: %s.%n", Arrays.toString(templates.stream().map(AppTemplate::name).toArray()), ",");
for (AppTemplate template: templates) {
System.out.println(template.manifestId());
}
Expand All @@ -57,6 +64,40 @@ public static void appsListByResourceGroup(IotCentralManager manager, String res
}
System.out.println();
}

/**
* Retrieves PrivateLink information about the IoT Central application.
**/
public static void appsGetPrivateLinks(
IotCentralManager manager,
String resourceGroupName,
String appName) {

PagedIterable < PrivateLinkResource > privateLinkList = manager.privateLinks().list(resourceGroupName, appName);

printHeader("Printing Available Private Links");
for (PrivateLinkResource privateLink: privateLinkList) {
System.out.println(privateLink.name() + " " + privateLink.id());
}
System.out.println();
}

/**
* Retrieves PrivateEndpointConnection information about the IoT Central application.
**/
public static void appsGetPrivateEndpointConnections(
IotCentralManager manager,
String resourceGroupName,
String appName) {

PagedIterable < PrivateEndpointConnection > privateEndpointConnectionList = manager.privateEndpointConnections().list(resourceGroupName, appName);

printHeader("Printing Available Private Endpoint Connections");
for (PrivateEndpointConnection priveateEndPointConncetion: privateEndpointConnectionList) {
System.out.println(priveateEndPointConncetion.name() + " " + priveateEndPointConncetion.id());
}
System.out.println();
}

/**
* Creates a new IoT Central application using the IoT Central Management Plane APIs
Expand Down Expand Up @@ -85,9 +126,7 @@ public static String appsCreateOrUpdate(
.withTemplate("iotc-distribution")
.create();

Thread.sleep(10000);
System.out.println("App created successfully.");

return randomizedAppName;
}

Expand All @@ -104,7 +143,15 @@ public static void appsGet(
com.azure.resourcemanager.iotcentral.models.App app =
manager.apps().getByResourceGroupWithResponse(resourceGroupName, appName, Context.NONE).getValue();

System.out.println("Name:" + app.name() + ", Location: " + app.regionName() + ", " + app.toString() + "\n");
boolean hasNetworkingRules = app.networkRuleSets().ipRules().isEmpty();
if(hasNetworkingRules) {
System.out.println("Name:" + app.name() + ", Display Name: " + app.displayName() +", Location: " + app.regionName() + ", " + app.toString());
} else {
String filterName = app.networkRuleSets().ipRules().get(0).filterName();
String ipMask = app.networkRuleSets().ipRules().get(0).ipMask();
System.out.println("Name:" + app.name() + ", Display Name: " + app.displayName() +", Location: " + app.regionName() + ", IP Rules: " + filterName + " : " + ipMask + ", " + app.toString());
}
System.out.println();
}

/**
Expand All @@ -117,16 +164,18 @@ public static void appsUpdate(

printHeader("Update App:");

NetworkRuleSets networkRuleSets = setNetworkRules();
App resource =
manager.apps().getByResourceGroupWithResponse(resourceGroupName, appName, Context.NONE).getValue();
resource
.update()
.withIdentity(
new SystemAssignedServiceIdentity().withType(SystemAssignedServiceIdentityType.SYSTEM_ASSIGNED))
.withDisplayName(appName + "- new display name")
.withPublicNetworkAccess(PublicNetworkAccess.ENABLED)
.withNetworkRuleSets(networkRuleSets)
.apply();

Thread.sleep(10000);
System.out.println("App updated successfully.\n");
}

Expand Down Expand Up @@ -186,7 +235,7 @@ public static void appsCreateOrUpdateWithInvalidGeoLocation(
* Validates error when using an invalid SKU. This change (lack of support for S1 sku) was introduced in 2021-06-01 APIs.
**/
public static void appsCreateOrUpdateWithInvalidSku(
com.azure.resourcemanager.iotcentral.IotCentralManager manager,
IotCentralManager manager,
String resourceGroupName,
String appName,
String skuName) {
Expand Down Expand Up @@ -251,6 +300,18 @@ public static void appsCreateOrUpdateThrowsWithSubscriptionLessAppAndF1Sku(
}
}

private static NetworkRuleSets setNetworkRules() {
NetworkRuleSetIpRule ipRules = new NetworkRuleSetIpRule()
.withFilterName("Localhost")
.withIpMask("127.0.0.1");
NetworkRuleSets setNetworkRules = new NetworkRuleSets()
.withApplyToDevices(true)
.withApplyToIoTCentral(false)
.withDefaultAction(NetworkAction.ALLOW)
.withIpRules(Arrays.asList(ipRules));
return setNetworkRules;
}

private static void printHeader(String headerName) {
System.out.println("------------------");
System.out.println(headerName);
Expand Down
Loading