Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add aws_appmesh_mesh resource #6708

Merged
merged 2 commits into from
Dec 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions aws/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/aws/aws-sdk-go/service/acmpca"
"github.com/aws/aws-sdk-go/service/apigateway"
"github.com/aws/aws-sdk-go/service/applicationautoscaling"
"github.com/aws/aws-sdk-go/service/appmesh"
"github.com/aws/aws-sdk-go/service/appsync"
"github.com/aws/aws-sdk-go/service/athena"
"github.com/aws/aws-sdk-go/service/autoscaling"
Expand Down Expand Up @@ -252,6 +253,7 @@ type AWSClient struct {
pricingconn *pricing.Pricing
pinpointconn *pinpoint.Pinpoint
workspacesconn *workspaces.WorkSpaces
appmeshconn *appmesh.AppMesh
}

func (c *AWSClient) S3() *s3.S3 {
Expand Down Expand Up @@ -583,6 +585,7 @@ func (c *Config) Client() (interface{}, error) {
client.pricingconn = pricing.New(sess)
client.pinpointconn = pinpoint.New(sess)
client.workspacesconn = workspaces.New(sess)
client.appmeshconn = appmesh.New(sess)

// Workaround for https://github.com/aws/aws-sdk-go/issues/1376
client.kinesisconn.Handlers.Retry.PushBack(func(r *request.Request) {
Expand Down
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ func Provider() terraform.ResourceProvider {
"aws_appautoscaling_target": resourceAwsAppautoscalingTarget(),
"aws_appautoscaling_policy": resourceAwsAppautoscalingPolicy(),
"aws_appautoscaling_scheduled_action": resourceAwsAppautoscalingScheduledAction(),
"aws_appmesh_mesh": resourceAwsAppmeshMesh(),
"aws_appsync_api_key": resourceAwsAppsyncApiKey(),
"aws_appsync_datasource": resourceAwsAppsyncDatasource(),
"aws_appsync_graphql_api": resourceAwsAppsyncGraphqlApi(),
Expand Down
109 changes: 109 additions & 0 deletions aws/resource_aws_appmesh_mesh.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package aws

import (
"fmt"
"log"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/appmesh"
"github.com/hashicorp/terraform/helper/schema"
)

func resourceAwsAppmeshMesh() *schema.Resource {
return &schema.Resource{
Create: resourceAwsAppmeshMeshCreate,
Read: resourceAwsAppmeshMeshRead,
Delete: resourceAwsAppmeshMeshDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"arn": {
Type: schema.TypeString,
Computed: true,
},

"created_date": {
Type: schema.TypeString,
Computed: true,
},

"last_updated_date": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func resourceAwsAppmeshMeshCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).appmeshconn

meshName := d.Get("name").(string)
req := &appmesh.CreateMeshInput{
MeshName: aws.String(meshName),
}

log.Printf("[DEBUG] Creating App Mesh service mesh: %#v", req)
_, err := conn.CreateMesh(req)
if err != nil {
return fmt.Errorf("error creating App Mesh service mesh: %s", err)
}

d.SetId(meshName)

return resourceAwsAppmeshMeshRead(d, meta)
}

func resourceAwsAppmeshMeshRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).appmeshconn

resp, err := conn.DescribeMesh(&appmesh.DescribeMeshInput{
MeshName: aws.String(d.Id()),
})
if err != nil {
if isAWSErr(err, "NotFoundException", "") {
log.Printf("[WARN] App Mesh service mesh (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("error reading App Mesh service mesh: %s", err)
}
if aws.StringValue(resp.Mesh.Status.Status) == appmesh.MeshStatusCodeDeleted {
log.Printf("[WARN] App Mesh service mesh (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

d.Set("name", resp.Mesh.MeshName)
d.Set("arn", resp.Mesh.Metadata.Arn)
d.Set("created_date", resp.Mesh.Metadata.CreatedAt.Format(time.RFC3339))
d.Set("last_updated_date", resp.Mesh.Metadata.LastUpdatedAt.Format(time.RFC3339))

return nil
}

func resourceAwsAppmeshMeshDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).appmeshconn

log.Printf("[DEBUG] App Mesh service mesh: %s", d.Id())
_, err := conn.DeleteMesh(&appmesh.DeleteMeshInput{
MeshName: aws.String(d.Id()),
})
if err != nil {
if isAWSErr(err, "NotFoundException", "") {
return nil
}
return fmt.Errorf("error deleting App Mesh service mesh: %s", err)
}

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

import (
"fmt"
"regexp"
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/appmesh"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

func testAccAwsAppmeshMesh_basic(t *testing.T) {
var mesh appmesh.MeshData
resourceName := "aws_appmesh_mesh.foo"
rName := fmt.Sprintf("tf-test-%d", acctest.RandInt())

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAppmeshMeshDestroy,
Steps: []resource.TestStep{
{
Config: testAccAppmeshMeshConfig(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAppmeshMeshExists(
resourceName, &mesh),
resource.TestCheckResourceAttr(
resourceName, "name", rName),
resource.TestCheckResourceAttrSet(
resourceName, "created_date"),
resource.TestCheckResourceAttrSet(
resourceName, "last_updated_date"),
resource.TestMatchResourceAttr(
resourceName, "arn", regexp.MustCompile(fmt.Sprintf("^arn:[^:]+:appmesh:[^:]+:\\d{12}:mesh/%s", rName))),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccCheckAppmeshMeshDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).appmeshconn

for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_appmesh_mesh" {
continue
}

_, err := conn.DescribeMesh(&appmesh.DescribeMeshInput{
MeshName: aws.String(rs.Primary.Attributes["name"]),
})
if err != nil {
if isAWSErr(err, "NotFoundException", "") {
return nil
}
return err
}
return fmt.Errorf("still exist.")
}

return nil
}

func testAccCheckAppmeshMeshExists(name string, v *appmesh.MeshData) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).appmeshconn

rs, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("Not found: %s", name)
}
if rs.Primary.ID == "" {
return fmt.Errorf("No ID is set")
}

resp, err := conn.DescribeMesh(&appmesh.DescribeMeshInput{
MeshName: aws.String(rs.Primary.Attributes["name"]),
})
if err != nil {
return err
}

*v = *resp.Mesh

return nil
}
}

func testAccAppmeshMeshConfig(name string) string {
return fmt.Sprintf(`
resource "aws_appmesh_mesh" "foo" {
name = "%s"
}
`, name)
}
25 changes: 25 additions & 0 deletions aws/resource_aws_appmesh_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package aws

import (
"testing"
)

func TestAccAWSAppmesh(t *testing.T) {
testCases := map[string]map[string]func(t *testing.T){
"Mesh": {
"basic": testAccAwsAppmeshMesh_basic,
},
}

for group, m := range testCases {
m := m
t.Run(group, func(t *testing.T) {
for name, tc := range m {
tc := tc
t.Run(name, func(t *testing.T) {
tc(t)
})
}
})
}
}
9 changes: 9 additions & 0 deletions website/aws.erb
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,15 @@
</ul>
</li>

<li<%= sidebar_current("docs-aws-resource-appmesh") %>>
<a href="#">AppMesh Resources</a>
<ul class="nav nav-visible">
<li<%= sidebar_current("docs-aws-resource-appmesh-mesh") %>>
<a href="/docs/providers/aws/r/appmesh_mesh.html">aws_appmesh_mesh</a>
</li>
</ul>
</li>

<li<%= sidebar_current("docs-aws-resource-appsync") %>>
<a href="#">AppSync Resources</a>
<ul class="nav nav-visible">
Expand Down
42 changes: 42 additions & 0 deletions website/docs/r/appmesh_mesh.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
layout: "aws"
page_title: "AWS: aws_appmesh_mesh"
sidebar_current: "docs-aws-resource-appmesh-mesh"
description: |-
Provides an AWS App Mesh service mesh resource.
---

# aws_appmesh_mesh

Provides an AWS App Mesh service mesh resource.

## Example Usage

```hcl
resource "aws_appmesh_mesh" "simple" {
name = "simpleapp"
}
```

## Argument Reference

The following arguments are supported:

* `name` - (Required) The name to use for the service mesh.

## Attributes Reference

In addition to all arguments above, the following attributes are exported:

* `id` - The ID of the service mesh.
* `arn` - The ARN of the service mesh.
* `created_date` - The creation date of the service mesh.
* `last_updated_date` - The last update date of the service mesh.

## Import

App Mesh service meshes can be imported using the `name`, e.g.

```
$ terraform import aws_appmesh_mesh.simple simpleapp
```