github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/cmd/roachprod/vm/azure/ids.go (about)

     1  // Copyright 2019 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package azure
    12  
    13  import (
    14  	"fmt"
    15  	"regexp"
    16  
    17  	"github.com/cockroachdb/errors"
    18  )
    19  
    20  // The Azure API embeds quite a bit of useful data in a resource ID,
    21  // however the golang SDK doesn't provide a built-in means of parsing
    22  // this back out.
    23  //
    24  // This file can go away if
    25  // https://github.com/Azure/azure-sdk-for-go/issues/3080
    26  // is solved.
    27  
    28  var azureIDPattern = regexp.MustCompile(
    29  	"/subscriptions/(.+)/resourceGroups/(.+)/providers/(.+?)/(.+?)/(.+)")
    30  
    31  type azureID struct {
    32  	provider      string
    33  	resourceGroup string
    34  	resourceName  string
    35  	resourceType  string
    36  	subscription  string
    37  }
    38  
    39  func (id azureID) String() string {
    40  	return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/%s/%s/%s",
    41  		id.subscription, id.resourceGroup, id.provider, id.resourceType, id.resourceName)
    42  }
    43  
    44  func parseAzureID(id string) (azureID, error) {
    45  	parts := azureIDPattern.FindStringSubmatch(id)
    46  	if len(parts) == 0 {
    47  		return azureID{}, errors.Errorf("could not parse Azure ID %q", id)
    48  	}
    49  	ret := azureID{
    50  		subscription:  parts[1],
    51  		resourceGroup: parts[2],
    52  		provider:      parts[3],
    53  		resourceType:  parts[4],
    54  		resourceName:  parts[5],
    55  	}
    56  	return ret, nil
    57  }