Skip to content

Commit

Permalink
Use Azure group and deployment name as host ID
Browse files Browse the repository at this point in the history
Group name and deployment name are now used
together to build Azure host ID. So that the
group name and deployment can be retrieved
easily later for status and delete operations.

Signed-off-by: Ze Chen <[email protected]>
  • Loading branch information
zechen0 authored and alexellis committed Aug 26, 2020
1 parent 544c0f4 commit bc36c27
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 17 deletions.
40 changes: 36 additions & 4 deletions pkg/provision/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"encoding/binary"
"encoding/json"
"fmt"
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2017-12-01/compute"
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2017-09-01/network"
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2017-05-10/resources"
Expand All @@ -17,6 +18,7 @@ import (
"io/ioutil"
"log"
"os"
"strings"
"unicode/utf16"
)

Expand Down Expand Up @@ -50,6 +52,26 @@ var fileToEnvMap = map[string]string{
"managementEndpointUrl": "ManagementEndpoint",
}

// buildAzureHostID creates an ID for Azure based upon the group name,
// and deployment name
func buildAzureHostID(groupName, deploymentName string) (id string) {
return fmt.Sprintf("%s|%s", groupName, deploymentName)
}

// get some required fields from the custom Azure host ID
func getAzureFieldsFromID(id string) (groupName, deploymentName string, err error) {
fields := strings.Split(id, "|")
err = nil
if len(fields) == 2 {
groupName = fields[0]
deploymentName = fields[1]
} else {
err = fmt.Errorf("could not get fields from custom ID: fields: %v", fields)
return "", "", err
}
return groupName, deploymentName, nil
}

// In case azure auth file is encoded as UTF-16 instead of UTF-8
func decodeAzureAuthContents(b []byte) ([]byte, error) {
reader, enc := utfbom.Skip(bytes.NewReader(b))
Expand Down Expand Up @@ -106,7 +128,7 @@ func (p *AzureProvisioner) Provision(host BasicHost) (*ProvisionedHost, error) {
log.Printf("Provisioning host with Azure\n")

p.resourceGroupName = "inlets-" + host.Name
p.deploymentName = "inlets-deploy-" + uuid.New().String()
p.deploymentName = "deployment-" + uuid.New().String()

log.Printf("Creating resource group %s", p.resourceGroupName)
group, err := createGroup(p, host)
Expand All @@ -122,7 +144,7 @@ func (p *AzureProvisioner) Provision(host BasicHost) (*ProvisionedHost, error) {
}
return &ProvisionedHost{
IP: "",
ID: p.resourceGroupName,
ID: buildAzureHostID(p.resourceGroupName, p.deploymentName),
Status: ActiveStatus,
}, nil
}
Expand All @@ -131,7 +153,13 @@ func (p *AzureProvisioner) Provision(host BasicHost) (*ProvisionedHost, error) {
func (p *AzureProvisioner) Status(id string) (*ProvisionedHost, error) {
deploymentsClient := resources.NewDeploymentsClient(p.subscriptionId)
deploymentsClient.Authorizer = p.authorizer
deployment, err := deploymentsClient.Get(p.ctx, p.resourceGroupName, p.deploymentName)

resourceGroupName, deploymentName, err := getAzureFieldsFromID(id)
if err != nil {
return nil, err
}

deployment, err := deploymentsClient.Get(p.ctx, resourceGroupName, deploymentName)
if err != nil {
return nil, err
}
Expand All @@ -156,7 +184,11 @@ func (p *AzureProvisioner) Status(id string) (*ProvisionedHost, error) {
func (p *AzureProvisioner) Delete(request HostDeleteRequest) error {
groupsClient := resources.NewGroupsClient(p.subscriptionId)
groupsClient.Authorizer = p.authorizer
_, err := groupsClient.Delete(p.ctx, request.ID)
resourceGroupName, _, err := getAzureFieldsFromID(request.ID)
if err != nil {
return err
}
_, err = groupsClient.Delete(p.ctx, resourceGroupName)
return err
}

Expand Down
40 changes: 27 additions & 13 deletions pkg/provision/azure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,33 @@ func Test_Azure_Auth_Contents_Invalid(t *testing.T) {
}
}

func Test_Azure_Build_Group_Deployment_Name(t *testing.T) {
hostID := buildAzureHostID("inlets-peaceful-chaum1", "deployments-e589c808-3936-4bd9-b558-36640eb98cb0")
if hostID != "inlets-peaceful-chaum1|deployments-e589c808-3936-4bd9-b558-36640eb98cb0" {
t.Errorf("want: inlets-peaceful-chaum1|deployments-e589c808-3936-4bd9-b558-36640eb98cb, but got: %s", hostID)
}
}

func Test_Azure_Parse_Group_Deployment_Name_Success(t *testing.T) {
resourceGroupName, deploymentName, err := getAzureFieldsFromID("inlets-peaceful-chaum1|deployments-e589c808-3936-4bd9-b558-36640eb98cb0")
if err != nil {
t.Errorf("want: nil, but got: %s", err.Error())
}
if resourceGroupName != "inlets-peaceful-chaum1" {
t.Errorf("want: inlets-peaceful-chaum1, but got: %s", resourceGroupName)
}
if deploymentName != "deployments-e589c808-3936-4bd9-b558-36640eb98cb0" {
t.Errorf("want: deployments-e589c808-3936-4bd9-b558-36640eb98cb0, but got: %s", deploymentName)
}
}

func Test_Azure_Parse_Group_Deployment_Name_Fail(t *testing.T) {
_, _, err := getAzureFieldsFromID("INVALID_ID")
if err == nil {
t.Errorf("want: error, but got nil")
}
}

func Test_Azure_Template(t *testing.T) {
want := map[string]interface{}{
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
Expand Down Expand Up @@ -241,19 +268,6 @@ func Test_Azure_Parameters(t *testing.T) {
},
"networkSecurityGroupRules": map[string]interface{}{
"value": []interface{}{
map[string]interface{}{
"name": "SSH",
"properties": map[string]interface{}{
"access": "Allow",
"destinationAddressPrefix": "*",
"destinationPortRange": "22",
"direction": "Inbound",
"priority": 300,
"protocol": "TCP",
"sourceAddressPrefix": "*",
"sourcePortRange": "*",
},
},
map[string]interface{}{
"name": "HTTPS",
"properties": map[string]interface{}{
Expand Down

0 comments on commit bc36c27

Please sign in to comment.