forked from hashicorp/terraform-provider-aws
/
resource_aws_service_discovery_http_namespace.go
130 lines (106 loc) · 3.65 KB
/
resource_aws_service_discovery_http_namespace.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package aws
import (
"fmt"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/servicediscovery"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAwsServiceDiscoveryHttpNamespace() *schema.Resource {
return &schema.Resource{
Create: resourceAwsServiceDiscoveryHttpNamespaceCreate,
Read: resourceAwsServiceDiscoveryHttpNamespaceRead,
Delete: resourceAwsServiceDiscoveryHttpNamespaceDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateServiceDiscoveryHttpNamespaceName,
},
"description": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"arn": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceAwsServiceDiscoveryHttpNamespaceCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).sdconn
name := d.Get("name").(string)
input := &servicediscovery.CreateHttpNamespaceInput{
Name: aws.String(name),
CreatorRequestId: aws.String(resource.UniqueId()),
}
if v, ok := d.GetOk("description"); ok {
input.Description = aws.String(v.(string))
}
resp, err := conn.CreateHttpNamespace(input)
if err != nil {
return fmt.Errorf("error creating Service Discovery HTTP Namespace (%s): %s", name, err)
}
stateConf := &resource.StateChangeConf{
Pending: []string{servicediscovery.OperationStatusSubmitted, servicediscovery.OperationStatusPending},
Target: []string{servicediscovery.OperationStatusSuccess},
Refresh: servicediscoveryOperationRefreshStatusFunc(conn, *resp.OperationId),
Timeout: 5 * time.Minute,
}
opresp, err := stateConf.WaitForState()
if err != nil {
return fmt.Errorf("error waiting for Service Discovery HTTP Namespace (%s) creation: %s", name, err)
}
d.SetId(*opresp.(*servicediscovery.GetOperationOutput).Operation.Targets["NAMESPACE"])
return resourceAwsServiceDiscoveryHttpNamespaceRead(d, meta)
}
func resourceAwsServiceDiscoveryHttpNamespaceRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).sdconn
input := &servicediscovery.GetNamespaceInput{
Id: aws.String(d.Id()),
}
resp, err := conn.GetNamespace(input)
if err != nil {
if isAWSErr(err, servicediscovery.ErrCodeNamespaceNotFound, "") {
d.SetId("")
return nil
}
return fmt.Errorf("error reading Service Discovery HTTP Namespace (%s): %s", d.Id(), err)
}
d.Set("name", resp.Namespace.Name)
d.Set("description", resp.Namespace.Description)
d.Set("arn", resp.Namespace.Arn)
return nil
}
func resourceAwsServiceDiscoveryHttpNamespaceDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).sdconn
input := &servicediscovery.DeleteNamespaceInput{
Id: aws.String(d.Id()),
}
resp, err := conn.DeleteNamespace(input)
if err != nil {
if isAWSErr(err, servicediscovery.ErrCodeNamespaceNotFound, "") {
d.SetId("")
return nil
}
return fmt.Errorf("error deleting Service Discovery HTTP Namespace (%s): %s", d.Id(), err)
}
stateConf := &resource.StateChangeConf{
Pending: []string{servicediscovery.OperationStatusSubmitted, servicediscovery.OperationStatusPending},
Target: []string{servicediscovery.OperationStatusSuccess},
Refresh: servicediscoveryOperationRefreshStatusFunc(conn, *resp.OperationId),
Timeout: 5 * time.Minute,
}
_, err = stateConf.WaitForState()
if err != nil {
return fmt.Errorf("error waiting for Service Discovery HTTP Namespace (%s) deletion: %s", d.Id(), err)
}
return nil
}