Skip to content

Commit

Permalink
ec_deployment: Add support for deployment aliases (#298)
Browse files Browse the repository at this point in the history
Adds support for deployment aliases on both the `ec_deployment` resource
and `ec_deployment` datasource.

Signed-off-by: Marc Lopez <marc5.12@outlook.com>
  • Loading branch information
marclop authored May 6, 2021
1 parent b2ad971 commit 2daa9af
Show file tree
Hide file tree
Showing 22 changed files with 213 additions and 96 deletions.
3 changes: 3 additions & 0 deletions .changelog/298.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:feature
resource/ec_deployment: Supports deployment aliases in a new top level field `alias`.
```
1 change: 1 addition & 0 deletions docs/data-sources/ec_deployment.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ data "ec_deployment" "example" {
~> **NOTE:** Depending on the deployment definition, some values may not be set.
These will not be available for interpolation.

* `alias` - Deployment alias.
* `healthy` - Overall health status of the deployment.
* `id` - The unique ID of the deployment.
* `name` - The name of the deployment.
Expand Down
1 change: 1 addition & 0 deletions docs/resources/ec_deployment.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ The following arguments are supported:
-> Read the [ESS stack version policy](https://www.elastic.co/guide/en/cloud/current/ec-version-policy.html#ec-version-policy-available) to understand which versions are available.

* `name` - (Optional) Name of the deployment.
* `alias` - (Optional) Deployment alias, affects the format of the resource URLs.
* `request_id` - (Optional) Request ID to set when you create the deployment. Use it only when previous attempts return an error and `request_id` is returned as part of the error.
* `elasticsearch` (Required) Elasticsearch cluster definition, can only be specified once. For multi-node Elasticsearch clusters, use multiple `topology` blocks.
* `kibana` (Optional) Kibana instance definition, can only be specified once.
Expand Down
8 changes: 5 additions & 3 deletions ec/acc/datasource_deployment_basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ func TestAccDatasourceDeployment_basic(t *testing.T) {
depsDatasourceName := "data.ec_deployments.query"
randomName := prefix + acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
secondRandomName := prefix + "-" + acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
randomAlias := "alias" + acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
depCfg := "testdata/datasource_deployment_basic.tf"
cfg := fixtureAccDeploymentDatasourceBasic(t, depCfg, randomName, secondRandomName, getRegion(), computeOpTemplate)
cfg := fixtureAccDeploymentDatasourceBasicAlias(t, depCfg, randomAlias, randomName, secondRandomName, getRegion(), computeOpTemplate)
var namePrefix = secondRandomName[:22]

resource.ParallelTest(t, resource.TestCase{
Expand All @@ -45,6 +46,7 @@ func TestAccDatasourceDeployment_basic(t *testing.T) {
Config: cfg,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(datasourceName, "name", resourceName, "name"),
resource.TestCheckResourceAttrPair(datasourceName, "alias", resourceName, "alias"),
resource.TestCheckResourceAttrPair(datasourceName, "region", resourceName, "region"),
resource.TestCheckResourceAttrPair(datasourceName, "deployment_template_id", resourceName, "deployment_template_id"),
resource.TestCheckResourceAttrPair(datasourceName, "traffic_filter.#", resourceName, "traffic_filter.#"),
Expand Down Expand Up @@ -118,7 +120,7 @@ func TestAccDatasourceDeployment_basic(t *testing.T) {
})
}

func fixtureAccDeploymentDatasourceBasic(t *testing.T, fileName, name, secondName, region, depTpl string) string {
func fixtureAccDeploymentDatasourceBasicAlias(t *testing.T, fileName, alias, name, secondName, region, depTpl string) string {
t.Helper()

deploymentTpl := setDefaultTemplate(region, depTpl)
Expand All @@ -127,6 +129,6 @@ func fixtureAccDeploymentDatasourceBasic(t *testing.T, fileName, name, secondNam
t.Fatal(err)
}
return fmt.Sprintf(string(b),
region, name, region, deploymentTpl, secondName, region, deploymentTpl, secondName, region, deploymentTpl,
region, name, region, deploymentTpl, alias, secondName, region, deploymentTpl, secondName, region, deploymentTpl,
)
}
26 changes: 24 additions & 2 deletions ec/acc/deployment_basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ func TestAccDeployment_basic_tf(t *testing.T) {
resName := "ec_deployment.basic"
randomName := prefix + acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
startCfg := "testdata/deployment_basic.tf"
randomAlias := "alias" + acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
trafficFilterCfg := "testdata/deployment_basic_with_traffic_filter_2.tf"
trafficFilterUpdateCfg := "testdata/deployment_basic_with_traffic_filter_3.tf"
cfg := fixtureAccDeploymentResourceBasicWithApps(t, startCfg, randomName, getRegion(), defaultTemplate)
cfg := fixtureAccDeploymentResourceBasicWithAppsAlias(t, startCfg, randomAlias, randomName, getRegion(), defaultTemplate)
cfgWithTrafficFilter := fixtureAccDeploymentResourceBasicWithTF(t, trafficFilterCfg, randomName, getRegion(), defaultTemplate)
cfgWithTrafficFilterUpdate := fixtureAccDeploymentResourceBasicWithTF(t, trafficFilterUpdateCfg, randomName, getRegion(), defaultTemplate)
deploymentVersion, err := latestStackVersion()
Expand All @@ -48,6 +49,7 @@ func TestAccDeployment_basic_tf(t *testing.T) {
{
Config: cfg,
Check: checkBasicDeploymentResource(resName, randomName, deploymentVersion,
resource.TestCheckResourceAttr(resName, "alias", randomAlias),
resource.TestCheckResourceAttr(resName, "apm.0.config.#", "0"),
resource.TestCheckResourceAttr(resName, "elasticsearch.0.config.#", "0"),
resource.TestCheckResourceAttr(resName, "enterprise_search.0.config.#", "0"),
Expand Down Expand Up @@ -82,7 +84,7 @@ func TestAccDeployment_basic_tf(t *testing.T) {
func TestAccDeployment_basic_config(t *testing.T) {
resName := "ec_deployment.basic"
randomName := prefix + acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
startCfg := "testdata/deployment_basic.tf"
startCfg := "testdata/deployment_basic_settings_config_1.tf"
settingsConfig := "testdata/deployment_basic_settings_config_2.tf"
cfg := fixtureAccDeploymentResourceBasicWithApps(t, startCfg, randomName, getRegion(), defaultTemplate)
settingsConfigCfg := fixtureAccDeploymentResourceBasicWithApps(t, settingsConfig, randomName, getRegion(), defaultTemplate)
Expand Down Expand Up @@ -149,6 +151,26 @@ func fixtureAccDeploymentResourceBasicWithApps(t *testing.T, fileName, name, reg
)
}

func fixtureAccDeploymentResourceBasicWithAppsAlias(t *testing.T, fileName, alias, name, region, depTpl string) string {
t.Helper()
requiresAPIConn(t)

deploymentTpl := setDefaultTemplate(region, depTpl)
// esIC is no longer needed
_, kibanaIC, apmIC, essIC, err := setInstanceConfigurations(deploymentTpl)
if err != nil {
t.Fatal(err)
}

b, err := os.ReadFile(fileName)
if err != nil {
t.Fatal(err)
}
return fmt.Sprintf(string(b),
region, alias, name, region, deploymentTpl, kibanaIC, apmIC, essIC,
)
}

func fixtureAccDeploymentResourceBasicWithTF(t *testing.T, fileName, name, region, depTpl string) string {
t.Helper()

Expand Down
1 change: 1 addition & 0 deletions ec/acc/testdata/datasource_deployment_basic.tf
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ resource "ec_deployment" "basic_observability" {
}

resource "ec_deployment" "basic_datasource" {
alias = "%s"
name = "%s"
region = "%s"
version = data.ec_stack.latest.version
Expand Down
1 change: 1 addition & 0 deletions ec/acc/testdata/deployment_basic.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ data "ec_stack" "latest" {
}

resource "ec_deployment" "basic" {
alias = "%s"
name = "%s"
region = "%s"
version = data.ec_stack.latest.version
Expand Down
36 changes: 36 additions & 0 deletions ec/acc/testdata/deployment_basic_settings_config_1.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
data "ec_stack" "latest" {
version_regex = "latest"
region = "%s"
}

resource "ec_deployment" "basic" {
name = "%s"
region = "%s"
version = data.ec_stack.latest.version
deployment_template_id = "%s"

elasticsearch {
topology {
id = "hot_content"
size = "1g"
}
}

kibana {
topology {
instance_configuration_id = "%s"
}
}

apm {
topology {
instance_configuration_id = "%s"
}
}

enterprise_search {
topology {
instance_configuration_id = "%s"
}
}
}
6 changes: 6 additions & 0 deletions ec/ecdatasource/deploymentdatasource/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ func modelToState(d *schema.ResourceData, res *models.DeploymentGetResponse) err
return err
}

if res.Alias != "" {
if err := d.Set("alias", res.Alias); err != nil {
return err
}
}

es := res.Resources.Elasticsearch[0]

if es.Region != nil {
Expand Down
2 changes: 2 additions & 0 deletions ec/ecdatasource/deploymentdatasource/datasource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func Test_modelToState(t *testing.T) {
args: args{
d: deploymentSchemaArg,
res: &models.DeploymentGetResponse{
Alias: "some-alias",
ID: &mock.ValidClusterID,
Healthy: ec.Bool(true),
Name: ec.String("my_deployment_name"),
Expand Down Expand Up @@ -145,6 +146,7 @@ func newSampleDeployment() map[string]interface{} {
return map[string]interface{}{
"id": mock.ValidClusterID,
"name": "my_deployment_name",
"alias": "some-alias",
"deployment_template_id": "aws-io-optimized",
"healthy": true,
"region": "us-east-1",
Expand Down
4 changes: 4 additions & 0 deletions ec/ecdatasource/deploymentdatasource/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ import (

func newSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"alias": {
Type: schema.TypeString,
Computed: true,
},
"healthy": {
Type: schema.TypeBool,
Computed: true,
Expand Down
1 change: 1 addition & 0 deletions ec/ecresource/deploymentresource/expanders.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
func createResourceToModel(d *schema.ResourceData, client *api.API) (*models.DeploymentCreateRequest, error) {
var result = models.DeploymentCreateRequest{
Name: d.Get("name").(string),
Alias: d.Get("alias").(string),
Resources: &models.DeploymentCreateResources{},
Settings: &models.DeploymentCreateSettings{},
Metadata: &models.DeploymentCreateMetadata{},
Expand Down
12 changes: 8 additions & 4 deletions ec/ecresource/deploymentresource/expanders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ func Test_createResourceToModel(t *testing.T) {
),
},
want: &models.DeploymentCreateRequest{
Name: "my_deployment_name",
Name: "my_deployment_name",
Alias: "my-deployment",
Settings: &models.DeploymentCreateSettings{
TrafficFilterSettings: &models.TrafficFilterSettings{
Rulesets: []string{"0.0.0.0/0", "192.168.10.0/24"},
Expand Down Expand Up @@ -379,7 +380,8 @@ func Test_createResourceToModel(t *testing.T) {
),
},
want: &models.DeploymentCreateRequest{
Name: "my_deployment_name",
Name: "my_deployment_name",
Alias: "my-deployment",
Settings: &models.DeploymentCreateSettings{
TrafficFilterSettings: &models.TrafficFilterSettings{
Rulesets: []string{"0.0.0.0/0", "192.168.10.0/24"},
Expand Down Expand Up @@ -815,7 +817,8 @@ func Test_createResourceToModel(t *testing.T) {
client: api.NewMock(mock.New200Response(ioOptimizedTpl())),
},
want: &models.DeploymentCreateRequest{
Name: "my_deployment_name",
Name: "my_deployment_name",
Alias: "my-deployment",
Settings: &models.DeploymentCreateSettings{
TrafficFilterSettings: &models.TrafficFilterSettings{
Rulesets: []string{"0.0.0.0/0", "192.168.10.0/24"},
Expand Down Expand Up @@ -941,7 +944,8 @@ func Test_createResourceToModel(t *testing.T) {
client: api.NewMock(mock.New200Response(ioOptimizedTpl())),
},
want: &models.DeploymentCreateRequest{
Name: "my_deployment_name",
Name: "my_deployment_name",
Alias: "my-deployment",
Settings: &models.DeploymentCreateSettings{
TrafficFilterSettings: &models.TrafficFilterSettings{
Rulesets: []string{"0.0.0.0/0", "192.168.10.0/24"},
Expand Down
4 changes: 4 additions & 0 deletions ec/ecresource/deploymentresource/flatteners.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ func modelToState(d *schema.ResourceData, res *models.DeploymentGetResponse, rem
return err
}

if err := d.Set("alias", res.Alias); err != nil {
return err
}

if res.Metadata != nil {
if err := d.Set("tags", flattenTags(res.Metadata.Tags)); err != nil {
return err
Expand Down
Loading

0 comments on commit 2daa9af

Please sign in to comment.