Skip to content

Commit

Permalink
Updated Azure SDK version and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
alfonsof committed Feb 5, 2022
1 parent e877750 commit 2f181ab
Show file tree
Hide file tree
Showing 6 changed files with 322 additions and 227 deletions.
66 changes: 41 additions & 25 deletions azurevm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,61 @@ This folder contains a Java application example that handles Virtual Machines on

* You must have a [Microsoft Azure](https://azure.microsoft.com/) subscription.

* You must have the following installed:
* Java Development Kit (JDK) 8
* Apache Maven 3
* Azure CLI

* The code was written for:

* Java 8
* Apache Maven 3
* Azure Management Libraries for Java
* Java 8
* Apache Maven 3
* Azure SDK for Java: New Management Libraries (com.azure)

## Using the code

* Configure your Azure access.
* Sign in Azure (Interactively).

The Azure CLI's default authentication method for logins uses a web browser and access token to sign in.

1. Run the Azure CLI login command.

```bash
az login
```

If the CLI can open your default browser, it will do so and load an Azure sign-in page.

Otherwise, open a browser page at https://aka.ms/devicelogin and enter the authorization code displayed in your terminal.

If no web browser is available or the web browser fails to open, use device code flow with az login --use-device-code.

You must create an Azure AD service principal in order to enable application to connect resources into Azure. The service principal grants your application to manage resources in your Azure subscription.
2. Sign in with your account credentials in the browser.

The Azure SDKs Libraries for Java allow you to use several authentication schemes.
Make sure you select your subscription by:

The application uses an authentication file for authenticating.
```bash
az account set --subscription <name or id>
```

The credentials are taken from `AZURE_AUTH_LOCATION` environment variable.
* Configure your AZURE_SUBSCRIPTION_ID environment variable.

You can create a service principal and generate this file using Azure CLI 2.0 or using the Azure cloud shell.
Set the `AZURE_SUBSCRIPTION_ID` environment variable with the Microsoft Azure subscription ID.

* Make sure you select your subscription by:
To set this variable on Linux, macOS, or Unix, use `export`:

```bash
az account set --subscription <name or id>
```
```bash
export AZURE_SUBSCRIPTION_ID=<SUBSCRIPTION_ID>
```

and you have the privileges to create service principals.
To set this variable on Windows, use `set`:

```bash
set AZURE_SUBSCRIPTION_ID=<SUBSCRIPTION_ID>
```

* Execute the following command for creating the service principal and the authentication file:

```bash
az ad sp create-for-rbac --sdk-auth > my.azureauth
```

* Set the `AZURE_AUTH_LOCATION` environment variable in your Operating System with the path of your authentication file.
You must replace the value of:

```bash
AZURE_AUTH_LOCATION = /path/to/my.azureauth
```
* `<SUBSCRIPTION_ID>` by the Microsoft Azure subscription ID (Ex.: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx").

* Run the code.

Expand Down
25 changes: 17 additions & 8 deletions azurevm/commands.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
In order to avoid an error message "Invalid signature file digest for Manifest main attributes", we have to remove from the jar file the files:
Errors caused by signed jars in dependencies.

META-INF/BCKEY.DSA
META-INF/BCKEY.SF
META-INF/MSFTSIG.RSA
META-INF/MSFTSIG.SF
We have to remove from the jar file the files:

We can exclude these files with "maven-shade-plugin".
META-INF/BCKEY.DSA
META-INF/BCKEY.SF
META-INF/MSFTSIG.RSA
META-INF/MSFTSIG.SF

In order to avoid error messages like:

"Invalid signature file digest for Manifest main attributes"
"Error: Could not find or load main class Main example.AzureVM"

We can exclude these files with "maven-shade-plugin" in "pom.xml" file.

Working with IntelliJ this functionality does not work.

So, we remove the files with one of these commands after the jar file is created:

$ zip -d out/artifacts/azurevm_jar/azurevm.jar META-INF/*.RSA META-INF/*.DSA META-INF/*.SF
- MacOS / Linux:
$ zip -d out/artifacts/azurevm_jar/azurevm.jar "META-INF/*.RSA" "META-INF/*.DSA" "META-INF/*.SF"

$ 7z d -tzip out/artifacts/azurevm_jar/azurevm.jar META-INF/*.RSA META-INF/*.DSA META-INF/*.SF
- 7-zip tool:
$ 7z d -tzip out/artifacts/azurevm_jar/azurevm.jar META-INF/*.RSA META-INF/*.DSA META-INF/*.SF
28 changes: 9 additions & 19 deletions azurevm/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.alfonsof.azureexamples</groupId>
<artifactId>azure-vm</artifactId>
<version>0.1.0</version>
<version>2.0.0</version>
<packaging>jar</packaging>

<properties>
Expand All @@ -17,32 +17,22 @@

<dependencies>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure</artifactId>
<version>1.12.0</version>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.4.3</version>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-mgmt-compute</artifactId>
<version>1.12.0</version>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-mgmt-resources</artifactId>
<version>1.12.0</version>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-mgmt-network</artifactId>
<version>1.12.0</version>
<groupId>com.azure.resourcemanager</groupId>
<artifactId>azure-resourcemanager</artifactId>
<version>2.11.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<version>3.8.1</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
Expand All @@ -51,7 +41,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.0.0</version>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
Expand Down
3 changes: 0 additions & 3 deletions azurevm/src/main/java/META-INF/MANIFEST.MF

This file was deleted.

4 changes: 2 additions & 2 deletions azurevm/src/main/java/example/AzureVM.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static void main(String[] args) throws IOException {
AzureVMHelper.listVMs();
break;
case 2: // Create VM
AzureVMHelper.createVM();
AzureVMHelper.runVM();
break;
case 3: // List VM
AzureVMHelper.listVM();
Expand Down Expand Up @@ -65,7 +65,7 @@ private static void printMenu() {
System.out.println("4 = Start Virtual Machine");
System.out.println("5 = Stop Virtual Machine");
System.out.println("6 = Restart Virtual Machine");
System.out.println("7 = Delete/Deallocate Virtual Machine");
System.out.println("7 = Deallocate/Delete Virtual Machine");
System.out.println("Enter an option?");
}

Expand Down
Loading

0 comments on commit 2f181ab

Please sign in to comment.