Skip to content

Commit

Permalink
Merge pull request #127 from Scalr/feature/SCALRCORE-21436
Browse files Browse the repository at this point in the history
SCALRCORE-21436 [provider] Scalr Provider > Provider configuration resource and data source
  • Loading branch information
emocharnik committed May 25, 2022
2 parents 3e35131 + 89a273c commit 8086a6a
Show file tree
Hide file tree
Showing 13 changed files with 1,869 additions and 37 deletions.
37 changes: 37 additions & 0 deletions docs/data-sources/scalr_provider_configuration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
layout: "scalr"
page_title: "Scalr: scalr_provider_configuration"
sidebar_current: "docs-datasource-scalr-provider-configuration"
description: |-
Get information on a provider configuration.
---

# scalr_provider_configuration Data Source

This data source is used to retrieve id of a single provider configuration by name or type.

## Example Usage

```hcl
data "scalr_provider_configuration" "aws_dev" {
name = "aws_dev"
}
data "scalr_provider_configuration" "azure" {
provider_name = "azurerm"
}
```

## Argument Reference

The following arguments are supported:

* `name` - (Optional) The name of a Scalr provider configuration.
* `provider_name` - (Optional) The name of a Terraform provider.
* `environment_id` - (Optional) ID of the environment, in the format `env-xxxxxxxx`.

## Attribute Reference

All arguments plus:

* `id` - The provider configuration ID, in the format `pcfg-xxxxxxxxxxx`.
37 changes: 37 additions & 0 deletions docs/data-sources/scalr_provider_configurations.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
layout: "scalr"
page_title: "Scalr: scalr_provider_configurations"
sidebar_current: "docs-datasource-scalr-provider-configurations"
description: |-
Get information on provider configurations.
---

# scalr_provider_configurations Data Source

This data source is used to retrieve list of provider configuration ids by name or type.

## Example Usage

```hcl
data "scalr_provider_configurations" "aws" {
name = "in:aws_dev,aws_demo,aws_prod"
}
data "scalr_provider_configurations" "google" {
provider_name = "google"
}
```

## Argument Reference

The following arguments are supported:

* `name` - (Optional) The query used in a Scalr provider configuration name filter.
* `provider_name` - (Optional) The name of a Terraform provider.
* `environment_id` - (Optional) ID of the environment, in the format `env-xxxxxxxx`.

## Attribute Reference

All arguments plus:

* `ids` - The list of provider configuration IDs, in the format [`pcfg-xxxxxxxxxxx`, `pcfg-yyyyyyyyy`].
117 changes: 117 additions & 0 deletions docs/resources/scalr_provider_configuration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
---
layout: "scalr"
page_title: "Scalr: scalr_provider_configuration"
sidebar_current: "docs-resource-scalr-provider-configuration"
description: |-
Manages provider configurations.
---

# scalr_provider_configuration Resource

Manage the state of provider configuraitons in Scalr. Creates, updates and destroy.

## Example Usage

Aws provider:

```hcl
resource "scalr_provider_configuration" "aws" {
name = "aws_dev_us_east_1"
account_id = "acc-xxxxxxxxx"
export_shell_variables = false
aws {
secret_key = "my-secret-key"
access_key = "my-access-key"
}
}
```

Google provider:

```hcl
resource "scalr_provider_configuration" "google" {
name = "google_main"
account_id = "acc-xxxxxxxxx"
google {
project = "my-project"
credentials = "my-credentials"
}
}
```

Azurerm provider:

```hcl
resource "scalr_provider_configuration" "azurerm" {
name = "azurerm"
account_id = "acc-xxxxxxxxx"
azurerm {
client_id = "my-client-id"
client_secret = "my-client-secret"
subscription_id = "my-subscription-id"
tenant_id = "my-tenant-id"
}
}
```

Other providers:

```hcl
resource "scalr_provider_configuration" "kubernetes" {
name = "k8s"
account_id = "acc-xxxxxxxxx"
custom {
provider_name = "kubernetes"
argument {
name = "host"
value = "my-host"
description = "The hostname (in form of URI) of the Kubernetes API."
}
argument {
name = "username"
value = "my-username"
}
argument {
name = "password"
value = "my-password"
sensitive = true
}
}
}
```

## Argument Reference

* `account_id` - (Required) The account that owns the variable, specified as an ID, in the format.
* `name` - (Required) The name of a Scalr provider configuration. This field is unique for the account.
* `export_shell_variables` - (Optional) Export provider variables into the run environment. This option is available only for built in providers.
* `aws` - (Optional) Settings for the aws provider configuraiton. Exactly one of the following attributes must be set: `aws`, `google`, `azurerm`, `custom`.
The `aws` block supports the following:
* `secret_key` - (Optional) AWS secret key.
* `access_key` - (Optional) AWS access key.
* `google` - (Optional) Settings for the google provider configuraiton. Exactly one of the following attributes must be set: `aws`, `google`, `azurerm`, `custom`.
The `google` block supports the following:
* `project` - (Optional) The default project to manage resources in. If another project is specified on a resource, it will take precedence.
* `credentials` - (Optional) Either the path to or the contents of a service account key file in JSON format. You can manage key files using the Cloud Console. If not provided, the application default credentials will be used.
* `azurerm` - (Optional) Settings for the azurerm provider configuraiton. Exactly one of the following attributes must be set: `aws`, `google`, `azurerm`, `custom`.
The `azurerm` block supports the following:
* `client_id` - (Optional) The Client ID which should be used.
* `client_secret` - (Optional) The Client Secret which should be used.
* `subscription_id` - (Optional) The Subscription ID which should be used.
* `tenant_id` - (Optional) The Tenant ID should be used.
* `custom` - (Optional) Settings for the provider configuraiton that does not have first class scalr support. Exactly one of the following attributes must be set: `aws`, `google`, `azurerm`, `custom`.
The `custom` block supports the following:
* `provider_name` - (Required) The name of a Terraform provider.
* `argument` - (Required) The provider configuration argument.
The `argument` block supports the following:
* `name` - (Required) The name of the provider configuration argument.
* `value` - (Optional) The value of the provider configuration argument.
* `sensitive` - (Optional) Set (true/false) to configure as sensitive. Default `false`.
* `description` - (Optional) The description of the provider configuration argument.


## Attribute Reference

All arguments plus:

* `id` - The ID of the provider configuration, in the format `pcfg-xxxxxxxx`.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require (
github.com/hashicorp/hcl v0.0.0-20180404174102-ef8a98b0bbce
github.com/hashicorp/terraform-plugin-sdk v1.17.2
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734
github.com/scalr/go-scalr v0.0.0-20220408125149-98e871983106
github.com/scalr/go-scalr v0.0.0-20220509095836-2578ecfec9e3
)

require (
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -299,10 +299,8 @@ github.com/posener/complete v1.2.1 h1:LrvDIY//XNo65Lq84G/akBuMGlawHvGBABv8f/ZN6D
github.com/posener/complete v1.2.1/go.mod h1:6gapUrK/U1TAN7ciCoNRIdVC5sbdBTUh1DKN0g6uH7E=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/scalr/go-scalr v0.0.0-20220331085920-4ec72236c661 h1:DnvLMEoIGdre8txCto7ml8mvI/LcIA+uqtdhXRDUY3o=
github.com/scalr/go-scalr v0.0.0-20220331085920-4ec72236c661/go.mod h1:xMnwfer9UxugeNITZjTpQBwQ/4bw6/JdyDLpGdmyorE=
github.com/scalr/go-scalr v0.0.0-20220408125149-98e871983106 h1:2eJf0QIg5LpWqRVULkePfcvofFKid+pcHVDyhf6Jnfw=
github.com/scalr/go-scalr v0.0.0-20220408125149-98e871983106/go.mod h1:xMnwfer9UxugeNITZjTpQBwQ/4bw6/JdyDLpGdmyorE=
github.com/scalr/go-scalr v0.0.0-20220509095836-2578ecfec9e3 h1:P03PrnjjQ4eG7E94FvOn0L40v1gpWiVVq1RfdmPWNzk=
github.com/scalr/go-scalr v0.0.0-20220509095836-2578ecfec9e3/go.mod h1:xMnwfer9UxugeNITZjTpQBwQ/4bw6/JdyDLpGdmyorE=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
Expand Down
2 changes: 0 additions & 2 deletions scalr/data_source_scalr_agent_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,11 @@ func dataSourceScalrAgentPool() *schema.Resource {
"account_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"environment_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},

"workspace_ids": {
Expand Down
68 changes: 68 additions & 0 deletions scalr/data_source_scalr_provider_configuration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package scalr

import (
"errors"
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"

scalr "github.com/scalr/go-scalr"
)

func dataSourceScalrProviderConfiguration() *schema.Resource {
return &schema.Resource{
Read: dataSourceScalrProviderConfigurationRead,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"account_id": {
Type: schema.TypeString,
Optional: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
},
"provider_name": {
Type: schema.TypeString,
Optional: true,
},
},
}
}

func dataSourceScalrProviderConfigurationRead(d *schema.ResourceData, meta interface{}) error {
scalrClient := meta.(*scalr.Client)

accountID := d.Get("account_id").(string)
name := d.Get("name").(string)
providerName := d.Get("provider_name").(string)

providersFilter := scalr.ProviderConfigurationFilter{
AccountID: accountID,
Name: name,
ProviderName: providerName,
}
options := scalr.ProviderConfigurationsListOptions{
Filter: &providersFilter,
}

providerConfigurations, err := scalrClient.ProviderConfigurations.List(ctx, options)
if err != nil {
return fmt.Errorf("Error retrieving provider configuration: %v", err)
}

if len(providerConfigurations.Items) > 1 {
return errors.New("Your query returned more than one result. Please try a more specific search criteria.")
}
if len(providerConfigurations.Items) == 0 {
return fmt.Errorf("Could not find provider configuration with name '%s', account_id: '%s', and provider_name: '%s'", name, accountID, providerName)
}

providerConfiguration := providerConfigurations.Items[0]
d.SetId(providerConfiguration.ID)

return nil
}
94 changes: 94 additions & 0 deletions scalr/data_source_scalr_provider_configuration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package scalr

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

func TestAccScalrProviderConfigurationDataSource_name(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccScalrProviderConfigurationAwsDataSourceInitConfig, // depends_on works improperly with data sources
},
{
Config: testAccScalrProviderConfigurationAwsDataSourceConfig,
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckEqualID("data.scalr_provider_configuration.aws", "scalr_provider_configuration.aws"),
),
},
{
Config: testAccScalrProviderConfigurationAwsDataSourceInitConfig,
},
},
})
}
func TestAccScalrProviderConfigurationDataSource_provider_name(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccScalrProviderConfigurationGoogleDataSourceInitConfig,
},
{
Config: testAccScalrProviderConfigurationGoogleDataSourceConfig,
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckEqualID("data.scalr_provider_configuration.google", "scalr_provider_configuration.google"),
),
},
{
Config: testAccScalrProviderConfigurationAwsDataSourceInitConfig,
},
},
})
}

var testAccScalrProviderConfigurationAwsDataSourceInitConfig = fmt.Sprintf(`
resource "scalr_provider_configuration" "google" {
name = "google_pcfg"
account_id = "%[1]s"
google {
project = "my-new-project"
credentials = "my-new-credentials"
}
}
resource "scalr_provider_configuration" "aws" {
name = "aws_pcfg"
account_id = "%[1]s"
aws {
secret_key = "my-new-secret-key"
access_key = "my-new-access-key"
}
}`, defaultAccount)
var testAccScalrProviderConfigurationAwsDataSourceConfig = testAccScalrProviderConfigurationAwsDataSourceInitConfig + `
data "scalr_provider_configuration" "aws" {
name = scalr_provider_configuration.aws.name
}
`

var testAccScalrProviderConfigurationGoogleDataSourceInitConfig = fmt.Sprintf(`
resource "scalr_provider_configuration" "google" {
name = "google_pcfg"
account_id = "%[1]s"
google {
project = "my-new-project"
credentials = "my-new-credentials"
}
}
resource "scalr_provider_configuration" "aws" {
name = "aws_pcfg"
account_id = "%[1]s"
aws {
secret_key = "my-new-secret-key"
access_key = "my-new-access-key"
}
}`, defaultAccount)
var testAccScalrProviderConfigurationGoogleDataSourceConfig = testAccScalrProviderConfigurationGoogleDataSourceInitConfig + `
data "scalr_provider_configuration" "google" {
provider_name = "google"
}`
Loading

0 comments on commit 8086a6a

Please sign in to comment.