forked from hashicorp/terraform-provider-aws
/
resource_aws_lightsail_static_ip_attachment.go
96 lines (81 loc) · 2.62 KB
/
resource_aws_lightsail_static_ip_attachment.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
package aws
import (
"log"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/lightsail"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAwsLightsailStaticIpAttachment() *schema.Resource {
return &schema.Resource{
Create: resourceAwsLightsailStaticIpAttachmentCreate,
Read: resourceAwsLightsailStaticIpAttachmentRead,
Delete: resourceAwsLightsailStaticIpAttachmentDelete,
Schema: map[string]*schema.Schema{
"static_ip_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"instance_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}
func resourceAwsLightsailStaticIpAttachmentCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).lightsailconn
staticIpName := d.Get("static_ip_name").(string)
log.Printf("[INFO] Attaching Lightsail Static IP: %q", staticIpName)
out, err := conn.AttachStaticIp(&lightsail.AttachStaticIpInput{
StaticIpName: aws.String(staticIpName),
InstanceName: aws.String(d.Get("instance_name").(string)),
})
if err != nil {
return err
}
log.Printf("[INFO] Lightsail Static IP attached: %s", *out)
d.SetId(staticIpName)
return resourceAwsLightsailStaticIpAttachmentRead(d, meta)
}
func resourceAwsLightsailStaticIpAttachmentRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).lightsailconn
staticIpName := d.Get("static_ip_name").(string)
log.Printf("[INFO] Reading Lightsail Static IP: %q", staticIpName)
out, err := conn.GetStaticIp(&lightsail.GetStaticIpInput{
StaticIpName: aws.String(staticIpName),
})
if err != nil {
if awsErr, ok := err.(awserr.Error); ok {
if awsErr.Code() == "NotFoundException" {
log.Printf("[WARN] Lightsail Static IP (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
}
return err
}
if !*out.StaticIp.IsAttached {
log.Printf("[WARN] Lightsail Static IP (%s) is not attached, removing from state", d.Id())
d.SetId("")
return nil
}
log.Printf("[INFO] Received Lightsail Static IP: %s", *out)
d.Set("instance_name", out.StaticIp.AttachedTo)
return nil
}
func resourceAwsLightsailStaticIpAttachmentDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).lightsailconn
name := d.Get("static_ip_name").(string)
log.Printf("[INFO] Detaching Lightsail Static IP: %q", name)
out, err := conn.DetachStaticIp(&lightsail.DetachStaticIpInput{
StaticIpName: aws.String(name),
})
if err != nil {
return err
}
log.Printf("[INFO] Detached Lightsail Static IP: %s", *out)
return nil
}