Skip to content

Commit

Permalink
Merge pull request #193 from hashicorp/f/common-ids-for-sql
Browse files Browse the repository at this point in the history
SQL Managed Instance
  • Loading branch information
tombuildsstuff authored Nov 6, 2023
2 parents 97adae4 + a525348 commit cf89ec7
Show file tree
Hide file tree
Showing 10 changed files with 2,219 additions and 0 deletions.
140 changes: 140 additions & 0 deletions resourcemanager/commonids/sql_database.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package commonids

import (
"fmt"
"strings"

"github.com/hashicorp/go-azure-helpers/resourcemanager/resourceids"
)

var _ resourceids.ResourceId = SqlDatabaseId{}

// SqlDatabaseId is a struct representing the Resource ID for a Sql Database
type SqlDatabaseId struct {
SubscriptionId string
ResourceGroupName string
ServerName string
DatabaseName string
}

// NewSqlDatabaseID returns a new SqlDatabaseId struct
func NewSqlDatabaseID(subscriptionId string, resourceGroupName string, serverName string, databaseName string) SqlDatabaseId {
return SqlDatabaseId{
SubscriptionId: subscriptionId,
ResourceGroupName: resourceGroupName,
ServerName: serverName,
DatabaseName: databaseName,
}
}

// ParseDatabaseID parses 'input' into a SqlDatabaseId
func ParseDatabaseID(input string) (*SqlDatabaseId, error) {
parser := resourceids.NewParserFromResourceIdType(SqlDatabaseId{})
parsed, err := parser.Parse(input, false)
if err != nil {
return nil, fmt.Errorf("parsing %q: %+v", input, err)
}

var ok bool
id := SqlDatabaseId{}

if id.SubscriptionId, ok = parsed.Parsed["subscriptionId"]; !ok {
return nil, resourceids.NewSegmentNotSpecifiedError(id, "subscriptionId", *parsed)
}

if id.ResourceGroupName, ok = parsed.Parsed["resourceGroupName"]; !ok {
return nil, resourceids.NewSegmentNotSpecifiedError(id, "resourceGroupName", *parsed)
}

if id.ServerName, ok = parsed.Parsed["serverName"]; !ok {
return nil, resourceids.NewSegmentNotSpecifiedError(id, "serverName", *parsed)
}

if id.DatabaseName, ok = parsed.Parsed["databaseName"]; !ok {
return nil, resourceids.NewSegmentNotSpecifiedError(id, "databaseName", *parsed)
}

return &id, nil
}

// ParseSqlDatabaseIDInsensitively parses 'input' case-insensitively into a SqlDatabaseId
// note: this method should only be used for API response data and not user input
func ParseSqlDatabaseIDInsensitively(input string) (*SqlDatabaseId, error) {
parser := resourceids.NewParserFromResourceIdType(SqlDatabaseId{})
parsed, err := parser.Parse(input, true)
if err != nil {
return nil, fmt.Errorf("parsing %q: %+v", input, err)
}

var ok bool
id := SqlDatabaseId{}

if id.SubscriptionId, ok = parsed.Parsed["subscriptionId"]; !ok {
return nil, resourceids.NewSegmentNotSpecifiedError(id, "subscriptionId", *parsed)
}

if id.ResourceGroupName, ok = parsed.Parsed["resourceGroupName"]; !ok {
return nil, resourceids.NewSegmentNotSpecifiedError(id, "resourceGroupName", *parsed)
}

if id.ServerName, ok = parsed.Parsed["serverName"]; !ok {
return nil, resourceids.NewSegmentNotSpecifiedError(id, "serverName", *parsed)
}

if id.DatabaseName, ok = parsed.Parsed["databaseName"]; !ok {
return nil, resourceids.NewSegmentNotSpecifiedError(id, "databaseName", *parsed)
}

return &id, nil
}

// ValidateSqlDatabaseID checks that 'input' can be parsed as a Sql Database ID
func ValidateSqlDatabaseID(input interface{}, key string) (warnings []string, errors []error) {
v, ok := input.(string)
if !ok {
errors = append(errors, fmt.Errorf("expected %q to be a string", key))
return
}

if _, err := ParseDatabaseID(v); err != nil {
errors = append(errors, err)
}

return
}

// ID returns the formatted Database ID
func (id SqlDatabaseId) ID() string {
fmtString := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Sql/servers/%s/databases/%s"
return fmt.Sprintf(fmtString, id.SubscriptionId, id.ResourceGroupName, id.ServerName, id.DatabaseName)
}

// Segments returns a slice of Resource ID Segments which comprise this Sql Database ID
func (id SqlDatabaseId) Segments() []resourceids.Segment {
return []resourceids.Segment{
resourceids.StaticSegment("staticSubscriptions", "subscriptions", "subscriptions"),
resourceids.SubscriptionIdSegment("subscriptionId", "12345678-1234-9876-4563-123456789012"),
resourceids.StaticSegment("staticResourceGroups", "resourceGroups", "resourceGroups"),
resourceids.ResourceGroupSegment("resourceGroupName", "example-resource-group"),
resourceids.StaticSegment("staticProviders", "providers", "providers"),
resourceids.ResourceProviderSegment("staticMicrosoftSql", "Microsoft.Sql", "Microsoft.Sql"),
resourceids.StaticSegment("staticServers", "servers", "servers"),
resourceids.UserSpecifiedSegment("serverName", "serverValue"),
resourceids.StaticSegment("staticDatabases", "databases", "databases"),
resourceids.UserSpecifiedSegment("databaseName", "databaseValue"),
}
}

// String returns a human-readable description of this Sql Database ID
func (id SqlDatabaseId) String() string {
components := []string{
fmt.Sprintf("Subscription: %q", id.SubscriptionId),
fmt.Sprintf("Resource Group Name: %q", id.ResourceGroupName),
fmt.Sprintf("Server Name: %q", id.ServerName),
fmt.Sprintf("Database Name: %q", id.DatabaseName),
}
return fmt.Sprintf("Database (%s)", strings.Join(components, "\n"))
}
Loading

0 comments on commit cf89ec7

Please sign in to comment.