Skip to content

Commit

Permalink
new resource azurerm_spring_cloud_app, `azurerm_spring_cloud_deploy…
Browse files Browse the repository at this point in the history
…ment` and `azurerm_spring_cloud_active_deployment`
  • Loading branch information
njuCZ committed Apr 7, 2020
1 parent c916aa6 commit 526e6b4
Show file tree
Hide file tree
Showing 43 changed files with 4,033 additions and 5 deletions.
47 changes: 47 additions & 0 deletions azurerm/internal/authorizers/authorizer_shared_access_signature.go
@@ -0,0 +1,47 @@
package authorizers

import (
"net/http"
"net/url"

"github.com/Azure/go-autorest/autorest"
)

// SharedAccessSignatureAuthorizer implements an authorization for Shared Access Signature
// this can be used for interaction with Blob, File and Queue Storage Endpoints
type SharedAccessSignatureAuthorizer struct {
sasQuery map[string]interface{}
}

// NewSharedAccessSignatureAuthorizer creates a SharedAccessSignatureAuthorizer using sasToken
func NewSharedAccessSignatureAuthorizer(sasToken string) *SharedAccessSignatureAuthorizer {
m, _ := url.ParseQuery(sasToken)
query := make(map[string]interface{}, len(m))
for key, value := range m {
for i, v := range value {
value[i] = url.QueryEscape(v)
}
query[key] = value
}
return &SharedAccessSignatureAuthorizer{
sasQuery: query,
}
}

// WithAuthorization returns a PrepareDecorator that adds an HTTP Authorization header whose
// value is "SharedKey " followed by the computed key.
// This can be used for the Blob, Queue, and File Services
//
// from: https://docs.microsoft.com/en-us/rest/api/storageservices/delegate-access-with-shared-access-signature
func (skl *SharedAccessSignatureAuthorizer) WithAuthorization() autorest.PrepareDecorator {
return func(p autorest.Preparer) autorest.Preparer {
return autorest.PreparerFunc(func(r *http.Request) (*http.Request, error) {
r, err := p.Prepare(r)
if err != nil {
return r, err
}

return autorest.Prepare(r, autorest.WithQueryParameters(skl.sasQuery))
})
}
}
14 changes: 12 additions & 2 deletions azurerm/internal/services/appplatform/client/client.go
Expand Up @@ -6,14 +6,24 @@ import (
)

type Client struct {
ServicesClient *appplatform.ServicesClient
ServicesClient *appplatform.ServicesClient
AppsClient *appplatform.AppsClient
DeploymentsClient *appplatform.DeploymentsClient
}

func NewClient(o *common.ClientOptions) *Client {
servicesClient := appplatform.NewServicesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&servicesClient.Client, o.ResourceManagerAuthorizer)

appsClient := appplatform.NewAppsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&appsClient.Client, o.ResourceManagerAuthorizer)

deploymentsClient := appplatform.NewDeploymentsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&deploymentsClient.Client, o.ResourceManagerAuthorizer)

return &Client{
ServicesClient: &servicesClient,
AppsClient: &appsClient,
DeploymentsClient: &deploymentsClient,
ServicesClient: &servicesClient,
}
}
38 changes: 38 additions & 0 deletions azurerm/internal/services/appplatform/parse/spring_cloud_app.go
@@ -0,0 +1,38 @@
package parse

import (
"fmt"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
)

type SpringCloudAppId struct {
ResourceGroup string
ServiceName string
Name string
}

func SpringCloudAppID(input string) (*SpringCloudAppId, error) {
id, err := azure.ParseAzureResourceID(input)
if err != nil {
return nil, fmt.Errorf("unable to parse Spring Cloud App ID %q: %+v", input, err)
}

app := SpringCloudAppId{
ResourceGroup: id.ResourceGroup,
}

if app.ServiceName, err = id.PopSegment("Spring"); err != nil {
return nil, err
}

if app.Name, err = id.PopSegment("apps"); err != nil {
return nil, err
}

if err := id.ValidateNoEmptySegments(input); err != nil {
return nil, err
}

return &app, nil
}
@@ -0,0 +1,88 @@
package parse

import (
"testing"
)

func TestSpringCloudAppID(t *testing.T) {
testData := []struct {
Name string
Input string
Expected *SpringCloudAppId
}{
{
Name: "Empty",
Input: "",
Expected: nil,
},
{
Name: "No Resource Groups Segment",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000",
Expected: nil,
},
{
Name: "No Resource Groups Value",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/",
Expected: nil,
},
{
Name: "Resource Group ID",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/foo/",
Expected: nil,
},
{
Name: "No Spring Value",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.AppPlatform/Spring/",
Expected: nil,
},
{
Name: "Missing Apps Segment",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.AppPlatform/Spring/spring1/",
Expected: nil,
},
{
Name: "Missing Apps Value",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.AppPlatform/Spring/spring1/apps/",
Expected: nil,
},
{
Name: "Spring Cloud App ID",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.AppPlatform/Spring/spring1/apps/app1",
Expected: &SpringCloudAppId{
ResourceGroup: "resGroup1",
ServiceName: "spring1",
Name: "app1",
},
},
{
Name: "Wrong Casing",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.AppPlatform/Spring/spring1/Apps/app1",
Expected: nil,
},
}

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

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

t.Fatalf("Expected a value but got an error: %s", err)
}

if actual.ResourceGroup != v.Expected.ResourceGroup {
t.Fatalf("Expected %q but got %q for Resource Group", v.Expected.ResourceGroup, actual.ResourceGroup)
}

if actual.ServiceName != v.Expected.ServiceName {
t.Fatalf("Expected %q but got %q for ServiceName", v.Expected.ServiceName, actual.ServiceName)
}

if actual.Name != v.Expected.Name {
t.Fatalf("Expected %q but got %q for Name", v.Expected.Name, actual.Name)
}
}
}
@@ -0,0 +1,43 @@
package parse

import (
"fmt"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
)

type SpringCloudDeploymentId struct {
ResourceGroup string
ServiceName string
AppName string
Name string
}

func SpringCloudDeploymentID(input string) (*SpringCloudDeploymentId, error) {
id, err := azure.ParseAzureResourceID(input)
if err != nil {
return nil, fmt.Errorf("unable to parse Spring Cloud Deployment ID %q: %+v", input, err)
}

deployment := SpringCloudDeploymentId{
ResourceGroup: id.ResourceGroup,
}

if deployment.ServiceName, err = id.PopSegment("Spring"); err != nil {
return nil, err
}

if deployment.AppName, err = id.PopSegment("apps"); err != nil {
return nil, err
}

if deployment.Name, err = id.PopSegment("deployments"); err != nil {
return nil, err
}

if err := id.ValidateNoEmptySegments(input); err != nil {
return nil, err
}

return &deployment, nil
}
@@ -0,0 +1,103 @@
package parse

import (
"testing"
)

func TestSpringCloudDeploymentID(t *testing.T) {
testData := []struct {
Name string
Input string
Expected *SpringCloudDeploymentId
}{
{
Name: "Empty",
Input: "",
Expected: nil,
},
{
Name: "No Resource Groups Segment",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000",
Expected: nil,
},
{
Name: "No Resource Groups Value",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/",
Expected: nil,
},
{
Name: "Resource Group ID",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/foo/",
Expected: nil,
},
{
Name: "No Spring Value",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.AppPlatform/Spring/",
Expected: nil,
},
{
Name: "Missing Apps Segment",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.AppPlatform/Spring/spring1/",
Expected: nil,
},
{
Name: "Missing Apps Value",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.AppPlatform/Spring/spring1/apps/",
Expected: nil,
},
{
Name: "Missing Deployment Segment",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.AppPlatform/Spring/spring1/apps/app1",
Expected: nil,
},
{
Name: "Missing Deployment Value",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.AppPlatform/Spring/spring1/apps/app1/deployments",
Expected: nil,
},
{
Name: "Spring Cloud Deployment ID",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.AppPlatform/Spring/spring1/apps/app1/deployments/deployment1",
Expected: &SpringCloudDeploymentId{
ResourceGroup: "resGroup1",
ServiceName: "spring1",
AppName: "app1",
Name: "deployment1",
},
},
{
Name: "Wrong Casing",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.AppPlatform/Spring/spring1/apps/app1/Deployments/deployment1",
Expected: nil,
},
}

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

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

t.Fatalf("Expected a value but got an error: %s", err)
}

if actual.ResourceGroup != v.Expected.ResourceGroup {
t.Fatalf("Expected %q but got %q for Resource Group", v.Expected.ResourceGroup, actual.ResourceGroup)
}

if actual.ServiceName != v.Expected.ServiceName {
t.Fatalf("Expected %q but got %q for ServiceName", v.Expected.ServiceName, actual.ServiceName)
}

if actual.AppName != v.Expected.AppName {
t.Fatalf("Expected %q but got %q for AppName", v.Expected.AppName, actual.AppName)
}

if actual.Name != v.Expected.Name {
t.Fatalf("Expected %q but got %q for Name", v.Expected.Name, actual.Name)
}
}
}
5 changes: 4 additions & 1 deletion azurerm/internal/services/appplatform/registration.go
Expand Up @@ -28,6 +28,9 @@ func (r Registration) SupportedDataSources() map[string]*schema.Resource {
// SupportedResources returns the supported Resources supported by this Service
func (r Registration) SupportedResources() map[string]*schema.Resource {
return map[string]*schema.Resource{
"azurerm_spring_cloud_service": resourceArmSpringCloudService(),
"azurerm_spring_cloud_active_deployment": resourceArmSpringCloudActiveDeployment(),
"azurerm_spring_cloud_app": resourceArmSpringCloudApp(),
"azurerm_spring_cloud_deployment": resourceArmSpringCloudDeployment(),
"azurerm_spring_cloud_service": resourceArmSpringCloudService(),
}
}

0 comments on commit 526e6b4

Please sign in to comment.