-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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_virtual_router resource #6720
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,159 @@ | ||||||
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 resourceAwsAppmeshVirtualRouter() *schema.Resource { | ||||||
return &schema.Resource{ | ||||||
Create: resourceAwsAppmeshVirtualRouterCreate, | ||||||
Read: resourceAwsAppmeshVirtualRouterRead, | ||||||
Update: resourceAwsAppmeshVirtualRouterUpdate, | ||||||
Delete: resourceAwsAppmeshVirtualRouterDelete, | ||||||
|
||||||
Schema: map[string]*schema.Schema{ | ||||||
"name": { | ||||||
Type: schema.TypeString, | ||||||
Required: true, | ||||||
ForceNew: true, | ||||||
}, | ||||||
|
||||||
"mesh_name": { | ||||||
Type: schema.TypeString, | ||||||
Required: true, | ||||||
ForceNew: true, | ||||||
}, | ||||||
|
||||||
"spec": { | ||||||
Type: schema.TypeList, | ||||||
Required: true, | ||||||
MinItems: 1, | ||||||
MaxItems: 1, | ||||||
Elem: &schema.Resource{ | ||||||
Schema: map[string]*schema.Schema{ | ||||||
"service_names": { | ||||||
Type: schema.TypeSet, | ||||||
Required: true, | ||||||
MinItems: 1, | ||||||
Elem: &schema.Schema{Type: schema.TypeString}, | ||||||
Set: schema.HashString, | ||||||
}, | ||||||
}, | ||||||
}, | ||||||
}, | ||||||
|
||||||
"arn": { | ||||||
Type: schema.TypeString, | ||||||
Computed: true, | ||||||
}, | ||||||
|
||||||
"created_date": { | ||||||
Type: schema.TypeString, | ||||||
Computed: true, | ||||||
}, | ||||||
|
||||||
"last_updated_date": { | ||||||
Type: schema.TypeString, | ||||||
Computed: true, | ||||||
}, | ||||||
}, | ||||||
} | ||||||
} | ||||||
|
||||||
func resourceAwsAppmeshVirtualRouterCreate(d *schema.ResourceData, meta interface{}) error { | ||||||
conn := meta.(*AWSClient).appmeshconn | ||||||
|
||||||
req := &appmesh.CreateVirtualRouterInput{ | ||||||
MeshName: aws.String(d.Get("mesh_name").(string)), | ||||||
VirtualRouterName: aws.String(d.Get("name").(string)), | ||||||
Spec: expandAppmeshVirtualRouterSpec(d.Get("spec").([]interface{})), | ||||||
} | ||||||
|
||||||
log.Printf("[DEBUG] Creating App Mesh virtual router: %#v", req) | ||||||
resp, err := conn.CreateVirtualRouter(req) | ||||||
if err != nil { | ||||||
return fmt.Errorf("error creating App Mesh virtual router: %s", err) | ||||||
} | ||||||
|
||||||
d.SetId(aws.StringValue(resp.VirtualRouter.Metadata.Uid)) | ||||||
|
||||||
return resourceAwsAppmeshVirtualRouterUpdate(d, meta) | ||||||
} | ||||||
|
||||||
func resourceAwsAppmeshVirtualRouterUpdate(d *schema.ResourceData, meta interface{}) error { | ||||||
conn := meta.(*AWSClient).appmeshconn | ||||||
|
||||||
if !d.IsNewResource() && d.HasChange("spec") { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Suggested change
|
||||||
_, v := d.GetChange("spec") | ||||||
req := &appmesh.UpdateVirtualRouterInput{ | ||||||
MeshName: aws.String(d.Get("mesh_name").(string)), | ||||||
VirtualRouterName: aws.String(d.Get("name").(string)), | ||||||
Spec: expandAppmeshVirtualRouterSpec(v.([]interface{})), | ||||||
} | ||||||
|
||||||
log.Printf("[DEBUG] Updating App Mesh virtual router: %#v", req) | ||||||
_, err := conn.UpdateVirtualRouter(req) | ||||||
if err != nil { | ||||||
return fmt.Errorf("error updating App Mesh virtual router: %s", err) | ||||||
} | ||||||
} | ||||||
|
||||||
return resourceAwsAppmeshVirtualRouterRead(d, meta) | ||||||
} | ||||||
|
||||||
func resourceAwsAppmeshVirtualRouterRead(d *schema.ResourceData, meta interface{}) error { | ||||||
conn := meta.(*AWSClient).appmeshconn | ||||||
|
||||||
resp, err := conn.DescribeVirtualRouter(&appmesh.DescribeVirtualRouterInput{ | ||||||
MeshName: aws.String(d.Get("mesh_name").(string)), | ||||||
VirtualRouterName: aws.String(d.Get("name").(string)), | ||||||
}) | ||||||
if err != nil { | ||||||
if isAWSErr(err, "NotFoundException", "") { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: SDK provided constant available:
Suggested change
|
||||||
log.Printf("[WARN] App Mesh virtual router (%s) not found, removing from state", d.Id()) | ||||||
d.SetId("") | ||||||
return nil | ||||||
} | ||||||
return fmt.Errorf("error reading App Mesh virtual router: %s", err) | ||||||
} | ||||||
if aws.StringValue(resp.VirtualRouter.Status.Status) == appmesh.VirtualRouterStatusCodeDeleted { | ||||||
log.Printf("[WARN] App Mesh virtual router (%s) not found, removing from state", d.Id()) | ||||||
d.SetId("") | ||||||
return nil | ||||||
} | ||||||
|
||||||
d.Set("name", resp.VirtualRouter.VirtualRouterName) | ||||||
d.Set("mesh_name", resp.VirtualRouter.MeshName) | ||||||
d.Set("arn", resp.VirtualRouter.Metadata.Arn) | ||||||
d.Set("created_date", resp.VirtualRouter.Metadata.CreatedAt.Format(time.RFC3339)) | ||||||
d.Set("last_updated_date", resp.VirtualRouter.Metadata.LastUpdatedAt.Format(time.RFC3339)) | ||||||
if err := d.Set("spec", flattenAppmeshVirtualRouterSpec(resp.VirtualRouter.Spec)); err != nil { | ||||||
return err | ||||||
} | ||||||
|
||||||
return nil | ||||||
} | ||||||
|
||||||
func resourceAwsAppmeshVirtualRouterDelete(d *schema.ResourceData, meta interface{}) error { | ||||||
conn := meta.(*AWSClient).appmeshconn | ||||||
|
||||||
log.Printf("[DEBUG] Deleting App Mesh virtual router: %s", d.Id()) | ||||||
_, err := conn.DeleteVirtualRouter(&appmesh.DeleteVirtualRouterInput{ | ||||||
MeshName: aws.String(d.Get("mesh_name").(string)), | ||||||
VirtualRouterName: aws.String(d.Get("name").(string)), | ||||||
}) | ||||||
if err != nil { | ||||||
if isAWSErr(err, "NotFoundException", "") { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: SDK provided constant available:
Suggested change
|
||||||
return nil | ||||||
} | ||||||
return fmt.Errorf("error deleting App Mesh virtual router: %s", err) | ||||||
} | ||||||
|
||||||
return nil | ||||||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,154 @@ | ||||||
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 testAccAwsAppmeshVirtualRouter_basic(t *testing.T) { | ||||||
var vr appmesh.VirtualRouterData | ||||||
resourceName := "aws_appmesh_virtual_router.foo" | ||||||
meshName := fmt.Sprintf("tf-test-mesh-%d", acctest.RandInt()) | ||||||
vrName := fmt.Sprintf("tf-test-router-%d", acctest.RandInt()) | ||||||
|
||||||
resource.Test(t, resource.TestCase{ | ||||||
PreCheck: func() { testAccPreCheck(t) }, | ||||||
Providers: testAccProviders, | ||||||
CheckDestroy: testAccCheckAppmeshVirtualRouterDestroy, | ||||||
Steps: []resource.TestStep{ | ||||||
{ | ||||||
Config: testAccAppmeshVirtualRouterConfig(meshName, vrName), | ||||||
Check: resource.ComposeTestCheckFunc( | ||||||
testAccCheckAppmeshVirtualRouterExists( | ||||||
resourceName, &vr), | ||||||
resource.TestCheckResourceAttr( | ||||||
resourceName, "name", vrName), | ||||||
resource.TestCheckResourceAttr( | ||||||
resourceName, "mesh_name", meshName), | ||||||
resource.TestCheckResourceAttr( | ||||||
resourceName, "spec.#", "1"), | ||||||
resource.TestCheckResourceAttr( | ||||||
resourceName, "spec.0.service_names.#", "1"), | ||||||
resource.TestCheckResourceAttr( | ||||||
resourceName, "spec.0.service_names.423761483", "serviceb.simpleapp.local"), | ||||||
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/virtualRouter/%s", meshName, vrName))), | ||||||
), | ||||||
}, | ||||||
{ | ||||||
Config: testAccAppmeshVirtualRouterConfig_serviceNamesUpdated(meshName, vrName), | ||||||
Check: resource.ComposeTestCheckFunc( | ||||||
testAccCheckAppmeshVirtualRouterExists( | ||||||
resourceName, &vr), | ||||||
resource.TestCheckResourceAttr( | ||||||
resourceName, "name", vrName), | ||||||
resource.TestCheckResourceAttr( | ||||||
resourceName, "mesh_name", meshName), | ||||||
resource.TestCheckResourceAttr( | ||||||
resourceName, "spec.#", "1"), | ||||||
resource.TestCheckResourceAttr( | ||||||
resourceName, "spec.0.service_names.#", "2"), | ||||||
resource.TestCheckResourceAttr( | ||||||
resourceName, "spec.0.service_names.3826429429", "serviceb1.simpleapp.local"), | ||||||
resource.TestCheckResourceAttr( | ||||||
resourceName, "spec.0.service_names.3079206513", "serviceb2.simpleapp.local"), | ||||||
), | ||||||
}, | ||||||
}, | ||||||
}) | ||||||
} | ||||||
|
||||||
func testAccCheckAppmeshVirtualRouterDestroy(s *terraform.State) error { | ||||||
conn := testAccProvider.Meta().(*AWSClient).appmeshconn | ||||||
|
||||||
for _, rs := range s.RootModule().Resources { | ||||||
if rs.Type != "aws_appmesh_virtual_router" { | ||||||
continue | ||||||
} | ||||||
|
||||||
_, err := conn.DescribeVirtualRouter(&appmesh.DescribeVirtualRouterInput{ | ||||||
MeshName: aws.String(rs.Primary.Attributes["mesh_name"]), | ||||||
VirtualRouterName: aws.String(rs.Primary.Attributes["name"]), | ||||||
}) | ||||||
if err != nil { | ||||||
if isAWSErr(err, "NotFoundException", "") { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: SDK provided constant available:
Suggested change
|
||||||
return nil | ||||||
} | ||||||
return err | ||||||
} | ||||||
return fmt.Errorf("still exist.") | ||||||
} | ||||||
|
||||||
return nil | ||||||
} | ||||||
|
||||||
func testAccCheckAppmeshVirtualRouterExists(name string, v *appmesh.VirtualRouterData) 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.DescribeVirtualRouter(&appmesh.DescribeVirtualRouterInput{ | ||||||
MeshName: aws.String(rs.Primary.Attributes["mesh_name"]), | ||||||
VirtualRouterName: aws.String(rs.Primary.Attributes["name"]), | ||||||
}) | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
|
||||||
*v = *resp.VirtualRouter | ||||||
|
||||||
return nil | ||||||
} | ||||||
} | ||||||
|
||||||
func testAccAppmeshVirtualRouterConfig(meshName, vrName string) string { | ||||||
return fmt.Sprintf(` | ||||||
resource "aws_appmesh_mesh" "foo" { | ||||||
name = "%s" | ||||||
} | ||||||
|
||||||
resource "aws_appmesh_virtual_router" "foo" { | ||||||
name = "%s" | ||||||
mesh_name = "${aws_appmesh_mesh.foo.id}" | ||||||
|
||||||
spec { | ||||||
service_names = ["serviceb.simpleapp.local"] | ||||||
} | ||||||
} | ||||||
`, meshName, vrName) | ||||||
} | ||||||
|
||||||
func testAccAppmeshVirtualRouterConfig_serviceNamesUpdated(meshName, vrName string) string { | ||||||
return fmt.Sprintf(` | ||||||
resource "aws_appmesh_mesh" "foo" { | ||||||
name = "%s" | ||||||
} | ||||||
|
||||||
resource "aws_appmesh_virtual_router" "foo" { | ||||||
name = "%s" | ||||||
mesh_name = "${aws_appmesh_mesh.foo.id}" | ||||||
|
||||||
spec { | ||||||
service_names = ["serviceb1.simpleapp.local", "serviceb2.simpleapp.local"] | ||||||
} | ||||||
} | ||||||
`, meshName, vrName) | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be calling the
Read
function instead?Spec
is already handled viaCreate