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

New resource/Data Source: azurerm_ssh_public_key #9842

Merged
merged 15 commits into from
Jan 7, 2021
Prev Previous commit
Next Next commit
make generate
Ali Allomani committed Dec 11, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 14b5b35e05ce82931ea41c07343dd24484b5f9c9
8 changes: 4 additions & 4 deletions azurerm/internal/services/compute/parse/ssh_public_key.go
Original file line number Diff line number Diff line change
@@ -15,21 +15,21 @@ type SSHPublicKeyId struct {
Name string
}

func NewSSHPublicKeyId(subscriptionId, resourceGroup, Name string) SSHPublicKeyId {
func NewSSHPublicKeyID(subscriptionId, resourceGroup, name string) SSHPublicKeyId {
return SSHPublicKeyId{
SubscriptionId: subscriptionId,
ResourceGroup: resourceGroup,
Name: Name,
Name: name,
}
}

func (id SSHPublicKeyId) String() string {
segments := []string{
fmt.Sprintf("Key Name %q", id.Name),
fmt.Sprintf("Name %q", id.Name),
fmt.Sprintf("Resource Group %q", id.ResourceGroup),
}
segmentsStr := strings.Join(segments, " / ")
return fmt.Sprintf("%s: (%s)", "SSH Public Key", segmentsStr)
return fmt.Sprintf("%s: (%s)", "S S H Public Key", segmentsStr)
}

func (id SSHPublicKeyId) ID() string {
112 changes: 112 additions & 0 deletions azurerm/internal/services/compute/parse/ssh_public_key_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package parse

// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten

import (
"testing"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/resourceid"
)

var _ resourceid.Formatter = SSHPublicKeyId{}

func TestSSHPublicKeyIDFormatter(t *testing.T) {
actual := NewSSHPublicKeyID("12345678-1234-9876-4563-123456789012", "resGroup1", "sshpublickey1").ID()
expected := "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Compute/sshPublicKeys/sshpublickey1"
if actual != expected {
t.Fatalf("Expected %q but got %q", expected, actual)
}
}

func TestSSHPublicKeyID(t *testing.T) {
testData := []struct {
Input string
Error bool
Expected *SSHPublicKeyId
}{

{
// empty
Input: "",
Error: true,
},

{
// missing SubscriptionId
Input: "/",
Error: true,
},

{
// missing value for SubscriptionId
Input: "/subscriptions/",
Error: true,
},

{
// missing ResourceGroup
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/",
Error: true,
},

{
// missing value for ResourceGroup
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/",
Error: true,
},

{
// missing Name
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Compute/",
Error: true,
},

{
// missing value for Name
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Compute/sshPublicKeys/",
Error: true,
},

{
// valid
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Compute/sshPublicKeys/sshpublickey1",
Expected: &SSHPublicKeyId{
SubscriptionId: "12345678-1234-9876-4563-123456789012",
ResourceGroup: "resGroup1",
Name: "sshpublickey1",
},
},

{
// upper-cased
Input: "/SUBSCRIPTIONS/12345678-1234-9876-4563-123456789012/RESOURCEGROUPS/RESGROUP1/PROVIDERS/MICROSOFT.COMPUTE/SSHPUBLICKEYS/SSHPUBLICKEY1",
Error: true,
},
}

for _, v := range testData {
t.Logf("[DEBUG] Testing %q", v.Input)

actual, err := SSHPublicKeyID(v.Input)
if err != nil {
if v.Error {
continue
}

t.Fatalf("Expect a value but got an error: %s", err)
}
if v.Error {
t.Fatal("Expect an error but didn't get one")
}

if actual.SubscriptionId != v.Expected.SubscriptionId {
t.Fatalf("Expected %q but got %q for SubscriptionId", v.Expected.SubscriptionId, actual.SubscriptionId)
}
if actual.ResourceGroup != v.Expected.ResourceGroup {
t.Fatalf("Expected %q but got %q for ResourceGroup", v.Expected.ResourceGroup, actual.ResourceGroup)
}
if actual.Name != v.Expected.Name {
t.Fatalf("Expected %q but got %q for Name", v.Expected.Name, actual.Name)
}
}
}
1 change: 1 addition & 0 deletions azurerm/internal/services/compute/resourceids.go
Original file line number Diff line number Diff line change
@@ -14,3 +14,4 @@ package compute
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=VirtualMachineExtension -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Compute/virtualMachines/machine1/extensions/extension1
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=VirtualMachineScaleSet -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Compute/virtualMachineScaleSets/scaleSet1
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=VirtualMachineScaleSetExtension -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Compute/virtualMachineScaleSets/scaleSet1/extensions/extension1
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=SSHPublicKey -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Compute/sshPublicKeys/sshpublickey1
23 changes: 23 additions & 0 deletions azurerm/internal/services/compute/validate/ssh_public_key_id.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package validate

// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten

import (
"fmt"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/compute/parse"
)

func SSHPublicKeyID(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 := parse.SSHPublicKeyID(v); err != nil {
errors = append(errors, err)
}

return
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package validate

// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten

import "testing"

func TestSSHPublicKeyID(t *testing.T) {
cases := []struct {
Input string
Valid bool
}{

{
// empty
Input: "",
Valid: false,
},

{
// missing SubscriptionId
Input: "/",
Valid: false,
},

{
// missing value for SubscriptionId
Input: "/subscriptions/",
Valid: false,
},

{
// missing ResourceGroup
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/",
Valid: false,
},

{
// missing value for ResourceGroup
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/",
Valid: false,
},

{
// missing Name
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Compute/",
Valid: false,
},

{
// missing value for Name
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Compute/sshPublicKeys/",
Valid: false,
},

{
// valid
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Compute/sshPublicKeys/sshpublickey1",
Valid: true,
},

{
// upper-cased
Input: "/SUBSCRIPTIONS/12345678-1234-9876-4563-123456789012/RESOURCEGROUPS/RESGROUP1/PROVIDERS/MICROSOFT.COMPUTE/SSHPUBLICKEYS/SSHPUBLICKEY1",
Valid: false,
},
}
for _, tc := range cases {
t.Logf("[DEBUG] Testing Value %s", tc.Input)
_, errors := SSHPublicKeyID(tc.Input, "test")
valid := len(errors) == 0

if tc.Valid != valid {
t.Fatalf("Expected %t but got %t", tc.Valid, valid)
}
}
}