|
| 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