From e75d8552b9904cf919fc9cfce199b7e04e2bb424 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Mon, 3 Dec 2018 16:21:39 -0500 Subject: [PATCH 1/2] Add aws_appmesh_mesh resource. --- aws/config.go | 3 + aws/provider.go | 1 + aws/resource_aws_appmesh_mesh.go | 109 ++++++++++++++++++++ aws/resource_aws_appmesh_mesh_test.go | 119 ++++++++++++++++++++++ website/aws.erb | 10 ++ website/docs/r/appmesh_mesh.html.markdown | 42 ++++++++ 6 files changed, 284 insertions(+) create mode 100644 aws/resource_aws_appmesh_mesh.go create mode 100644 aws/resource_aws_appmesh_mesh_test.go create mode 100644 website/docs/r/appmesh_mesh.html.markdown diff --git a/aws/config.go b/aws/config.go index 6b9d7ddecff..35b9cc1099b 100644 --- a/aws/config.go +++ b/aws/config.go @@ -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" @@ -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 { @@ -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) { diff --git a/aws/provider.go b/aws/provider.go index c884206acb9..9e101e37870 100644 --- a/aws/provider.go +++ b/aws/provider.go @@ -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(), diff --git a/aws/resource_aws_appmesh_mesh.go b/aws/resource_aws_appmesh_mesh.go new file mode 100644 index 00000000000..5c221b420bf --- /dev/null +++ b/aws/resource_aws_appmesh_mesh.go @@ -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 +} diff --git a/aws/resource_aws_appmesh_mesh_test.go b/aws/resource_aws_appmesh_mesh_test.go new file mode 100644 index 00000000000..195ef9ecab1 --- /dev/null +++ b/aws/resource_aws_appmesh_mesh_test.go @@ -0,0 +1,119 @@ +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_importBasic(t *testing.T) { + 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), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +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))), + ), + }, + }, + }) +} + +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) +} diff --git a/website/aws.erb b/website/aws.erb index 834d0cb2dbd..b5495b199f1 100644 --- a/website/aws.erb +++ b/website/aws.erb @@ -515,6 +515,16 @@ + > + AppSync Resources + + + > AppSync Resources