Skip to content

Commit 2322071

Browse files
committed
v0.3.0 release (increase Purity version for cbs_array_azure)
1 parent bc07d45 commit 2322071

19 files changed

+831
-231
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,8 @@
1818
.terraform
1919
*.tfstate
2020
*.tfstate.*
21+
**/.terraform.lock.hcl
22+
23+
/.build/
24+
**/.vscode/
25+

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.3.0 (April 13, 2021)
2+
3+
* Update Purity version of cbs_array_azure from 6.1.0 to 6.1.4
4+
15
## 0.2.0 (March 9, 2021)
26

37
* Release of CBS on Azure support through the cbs_array_azure resource

Makefile

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,36 @@
1-
default: install
1+
DEV_PKGDIR := $(HOME)/.terraform.d/plugins/terraform.purestorage.com/flasharray/cbs/
2+
DEV_GOBIN := $(DEV_PKGDIR)/99.99/linux_amd64/
3+
4+
default: build
25

36
build:
47
go build
58

6-
install:
7-
go install
8-
99
testacc:
1010
TF_ACC=1 go test ./cbs -v -timeout 120m
11+
12+
install-dev-mock:
13+
GOBIN=$(DEV_GOBIN) go install --tags mock
14+
15+
install-dev:
16+
GOBIN=$(DEV_GOBIN) go install
17+
18+
install-dev-clean:
19+
rm -rvf $(DEV_PKGDIR)
20+
21+
test-acc-mock:
22+
set -a; . testing/mock.env; go test --tags mock,mock_trace ./cbs -v -timeout 120m
23+
24+
test-vet:
25+
go vet ./cbs
26+
go vet -tags mock ./cbs
27+
28+
# Tests that should run on each pull request
29+
test-pull-request: test-vet test-acc-mock
30+
31+
tidy:
32+
go fmt ./cbs
33+
go mod tidy -v
34+
go fix ./cbs
35+
go clean ./cbs
36+
go clean --tags mock ./cbs

cbs/cbs_service.go

Lines changed: 30 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -19,34 +19,37 @@
1919
package cbs
2020

2121
import (
22+
"context"
2223
"fmt"
2324

2425
"github.com/Azure/azure-sdk-for-go/services/graphrbac/1.6/graphrbac"
2526
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2019-07-01/managedapplications"
26-
"github.com/Azure/go-autorest/autorest"
2727

2828
"github.com/aws/aws-sdk-go/aws"
29-
"github.com/aws/aws-sdk-go/aws/session"
29+
"github.com/aws/aws-sdk-go/aws/request"
3030
"github.com/aws/aws-sdk-go/service/cloudformation"
3131

32-
"github.com/hashicorp/go-azure-helpers/authentication"
33-
"github.com/hashicorp/go-azure-helpers/sender"
3432
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
3533
)
3634

37-
type CbsService struct {
38-
CloudFormation *cloudformation.CloudFormation
39-
AzureClient *AzureClient
35+
type cloudformationAPI interface {
36+
CreateStack(input *cloudformation.CreateStackInput) (*cloudformation.CreateStackOutput, error)
37+
DescribeStacks(input *cloudformation.DescribeStacksInput) (*cloudformation.DescribeStacksOutput, error)
38+
DeleteStack(input *cloudformation.DeleteStackInput) (*cloudformation.DeleteStackOutput, error)
39+
WaitUntilStackCreateCompleteWithContext(ctx aws.Context, input *cloudformation.DescribeStacksInput, opts ...request.WaiterOption) error
40+
WaitUntilStackDeleteCompleteWithContext(ctx aws.Context, input *cloudformation.DescribeStacksInput, opts ...request.WaiterOption) error
4041
}
4142

42-
type AzureClient struct {
43-
ApplicationsClient *managedapplications.ApplicationsClient
44-
GroupsClient *graphrbac.GroupsClient
43+
type CbsService struct {
44+
CloudFormation cloudformationAPI
45+
AzureClient AzureClientAPI
46+
awsRegionStr string
47+
azureConfig azureUserConfig
4548
}
4649

47-
func (m *CbsService) CloudFormationService() (*cloudformation.CloudFormation, diag.Diagnostics) {
50+
func (m *CbsService) CloudFormationService() (cloudformationAPI, diag.Diagnostics) {
4851
if m.CloudFormation == nil {
49-
cftSvc, diags := buildSession(awsRegionStr)
52+
cftSvc, diags := buildAWSSession(m.awsRegionStr)
5053
if diags.HasError() {
5154
return nil, diags
5255
}
@@ -56,9 +59,9 @@ func (m *CbsService) CloudFormationService() (*cloudformation.CloudFormation, di
5659
return m.CloudFormation, nil
5760
}
5861

59-
func (m *CbsService) AzureClientService() (*AzureClient, diag.Diagnostics) {
62+
func (m *CbsService) AzureClientService() (AzureClientAPI, diag.Diagnostics) {
6063
if m.AzureClient == nil {
61-
azureClient, diags := buildAzureClient(azureBuilder)
64+
azureClient, diags := buildAzureClient(m.azureConfig)
6265
if diags.HasError() {
6366
return nil, diags
6467
}
@@ -68,67 +71,28 @@ func (m *CbsService) AzureClientService() (*AzureClient, diag.Diagnostics) {
6871
return m.AzureClient, nil
6972
}
7073

71-
func buildSession(region string) (*cloudformation.CloudFormation, diag.Diagnostics) {
74+
func buildAWSSessionPreCheck(region string) diag.Diagnostics {
7275
var diags diag.Diagnostics
7376
if region == "" {
7477
diags = append(diags, diag.Diagnostic{
7578
Severity: diag.Error,
7679
Summary: fmt.Sprintf("No AWS region specified. The AWS region must be provided either in "+
7780
"the provider configuration block or with the %s environment variable.", awsRegionVar),
7881
})
79-
return nil, diags
82+
return diags
8083
}
81-
sess, err := session.NewSession(&aws.Config{
82-
Region: aws.String(region)},
83-
)
84-
if err != nil {
85-
return nil, diag.FromErr(err)
86-
}
87-
88-
cftSvc := cloudformation.New(sess)
89-
return cftSvc, nil
84+
return nil
9085
}
9186

92-
func buildAzureClient(builder *authentication.Builder) (*AzureClient, diag.Diagnostics) {
93-
var azureClient AzureClient
94-
config, err := builder.Build()
95-
if err != nil {
96-
return nil, diag.FromErr(err)
97-
}
98-
99-
env, err := authentication.DetermineEnvironment(config.Environment)
100-
if err != nil {
101-
return nil, diag.FromErr(err)
102-
}
103-
104-
// This indicates that a 429 response should not be included as a retry attempt
105-
// so that we continue to retry until it succeeds. Set this behavior to keep
106-
// consistent with azurerm provider.
107-
autorest.Count429AsRetry = false
108-
109-
oauthConfig, err := config.BuildOAuthConfig(env.ActiveDirectoryEndpoint)
110-
if err != nil {
111-
return nil, diag.FromErr(err)
112-
}
113-
sender := sender.BuildSender("cbs")
114-
auth, err := config.GetAuthorizationToken(sender, oauthConfig, env.TokenAudience)
115-
if err != nil {
116-
return nil, diag.FromErr(err)
117-
}
118-
graphAuth, err := config.GetAuthorizationToken(sender, oauthConfig, env.GraphEndpoint)
119-
if err != nil {
120-
return nil, diag.FromErr(err)
121-
}
87+
type AzureClientAPI interface {
88+
SubscriptionID() string
89+
groupsListComplete(ctx context.Context, filter string) (*[]graphrbac.ADGroup, error)
90+
appsCreateOrUpdate(ctx context.Context, resourceGroupName string, applicationName string, parameters managedapplications.Application) error
91+
appsGet(ctx context.Context, resourceGroupName string, applicationName string) (managedapplications.Application, error)
92+
appsDelete(ctx context.Context, resourceGroupName string, applicationName string) error
93+
}
12294

123-
// Create applications client
124-
client := managedapplications.NewApplicationsClient(config.SubscriptionID)
125-
client.SubscriptionID = config.SubscriptionID
126-
client.Client.Authorizer = auth
127-
// Create groups client
128-
groupClient := graphrbac.NewGroupsClient(config.TenantID)
129-
groupClient.Client.Authorizer = graphAuth
130-
131-
azureClient.ApplicationsClient = &client
132-
azureClient.GroupsClient = &groupClient
133-
return &azureClient, nil
95+
type AzureClient struct {
96+
ApplicationsClient *managedapplications.ApplicationsClient
97+
GroupsClient *graphrbac.GroupsClient
13498
}

cbs/cloud_services.go

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
// +build !mock
2+
3+
/*
4+
5+
Copyright 2021, Pure Storage Inc.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
20+
This file contains wrappers for "real" cloud service calls, as opposed to alternative
21+
implementations that are mocked. Some of these wrappers also do a small amount of
22+
"flattening" in order to simplify some of the cloud APIs. Simplifications includes
23+
reducing the number of objects, and putting multiple calls together. This helps serve
24+
as a single file for tracking all cloud accesses, and it also makes it clearer what
25+
interfaces need to be mocked.
26+
27+
*/
28+
29+
package cbs
30+
31+
import (
32+
"context"
33+
"fmt"
34+
35+
"github.com/Azure/azure-sdk-for-go/profiles/latest/resources/mgmt/managedapplications"
36+
"github.com/Azure/azure-sdk-for-go/services/graphrbac/1.6/graphrbac"
37+
"github.com/Azure/go-autorest/autorest"
38+
"github.com/aws/aws-sdk-go/aws"
39+
"github.com/aws/aws-sdk-go/aws/session"
40+
"github.com/aws/aws-sdk-go/service/cloudformation"
41+
"github.com/hashicorp/go-azure-helpers/authentication"
42+
"github.com/hashicorp/go-azure-helpers/sender"
43+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
44+
)
45+
46+
// Aws things:
47+
48+
func buildAWSSession(region string) (*cloudformation.CloudFormation, diag.Diagnostics) {
49+
var diags = buildAWSSessionPreCheck(region)
50+
if diags != nil {
51+
return nil, diags
52+
}
53+
sess, err := session.NewSession(&aws.Config{
54+
Region: aws.String(region)},
55+
)
56+
if err != nil {
57+
return nil, diag.FromErr(err)
58+
}
59+
60+
cftSvc := cloudformation.New(sess)
61+
return cftSvc, nil
62+
}
63+
64+
// Azure things:
65+
66+
func buildAzureClient(userConfig azureUserConfig) (AzureClientAPI, diag.Diagnostics) {
67+
68+
builder := &authentication.Builder{
69+
SubscriptionID: userConfig.SubscriptionID,
70+
ClientID: userConfig.ClientID,
71+
ClientSecret: userConfig.ClientSecret,
72+
TenantID: userConfig.TenantID,
73+
Environment: azureEnvironment,
74+
SupportsClientSecretAuth: true,
75+
SupportsAzureCliToken: true,
76+
}
77+
78+
var azureClient AzureClient
79+
config, err := builder.Build()
80+
if err != nil {
81+
return nil, diag.FromErr(err)
82+
}
83+
84+
env, err := authentication.DetermineEnvironment(config.Environment)
85+
if err != nil {
86+
return nil, diag.FromErr(err)
87+
}
88+
89+
// This indicates that a 429 response should not be included as a retry attempt
90+
// so that we continue to retry until it succeeds. Set this behavior to keep
91+
// consistent with azurerm provider.
92+
autorest.Count429AsRetry = false
93+
94+
oauthConfig, err := config.BuildOAuthConfig(env.ActiveDirectoryEndpoint)
95+
if err != nil {
96+
return nil, diag.FromErr(err)
97+
}
98+
sender := sender.BuildSender("cbs")
99+
auth, err := config.GetAuthorizationToken(sender, oauthConfig, env.TokenAudience)
100+
if err != nil {
101+
return nil, diag.FromErr(err)
102+
}
103+
graphAuth, err := config.GetAuthorizationToken(sender, oauthConfig, env.GraphEndpoint)
104+
if err != nil {
105+
return nil, diag.FromErr(err)
106+
}
107+
108+
// Create applications client
109+
applicationsClient := managedapplications.NewApplicationsClient(config.SubscriptionID)
110+
applicationsClient.SubscriptionID = config.SubscriptionID
111+
applicationsClient.Client.Authorizer = auth
112+
// Create groups client
113+
groupClient := graphrbac.NewGroupsClient(config.TenantID)
114+
groupClient.Client.Authorizer = graphAuth
115+
116+
azureClient.ApplicationsClient = &applicationsClient
117+
azureClient.GroupsClient = &groupClient
118+
return &azureClient, nil
119+
}
120+
121+
func (client *AzureClient) SubscriptionID() string {
122+
return client.ApplicationsClient.SubscriptionID
123+
}
124+
125+
func (client *AzureClient) groupsListComplete(ctx context.Context, filter string) (*[]graphrbac.ADGroup, error) {
126+
resp, err := client.GroupsClient.ListComplete(ctx, filter)
127+
if err != nil {
128+
return nil, err
129+
}
130+
return resp.Response().Value, nil
131+
}
132+
133+
func (azureClient *AzureClient) appsCreateOrUpdate(ctx context.Context, resourceGroupName string, applicationName string, parameters managedapplications.Application) error {
134+
future, err := azureClient.ApplicationsClient.CreateOrUpdate(ctx, resourceGroupName, applicationName, parameters)
135+
if err != nil {
136+
return fmt.Errorf("failed to create Managed Application %q (Resource Group %q): %+v", applicationName, resourceGroupName, err)
137+
}
138+
err = future.WaitForCompletionRef(ctx, azureClient.ApplicationsClient.Client)
139+
if err != nil {
140+
return fmt.Errorf("failed to wait for creation of Managed Application %q (Resource Group %q): %+v", applicationName, resourceGroupName, err)
141+
}
142+
return nil
143+
}
144+
145+
func (azureClient *AzureClient) appsGet(ctx context.Context, resourceGroupName string, applicationName string) (managedapplications.Application, error) {
146+
return azureClient.ApplicationsClient.Get(ctx, resourceGroupName, applicationName)
147+
}
148+
149+
func (azureClient *AzureClient) appsDelete(ctx context.Context, resourceGroupName string, applicationName string) error {
150+
future, err := azureClient.ApplicationsClient.Delete(ctx, resourceGroupName, applicationName)
151+
if err != nil {
152+
return fmt.Errorf("failed to delete Managed Application %q (Resource Group %q): %+v", applicationName, resourceGroupName, err)
153+
}
154+
err = future.WaitForCompletionRef(ctx, azureClient.ApplicationsClient.Client)
155+
if err != nil {
156+
return fmt.Errorf("failed to wait for deleting Managed Application (Managed Application Name %q / Resource Group %q): %+v", applicationName, resourceGroupName, err)
157+
}
158+
return nil
159+
}

0 commit comments

Comments
 (0)