forked from terraform-providers/terraform-provider-opc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_ip_association.go
103 lines (84 loc) · 2.35 KB
/
resource_ip_association.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
package opc
import (
"fmt"
"github.com/hashicorp/go-oracle-terraform/client"
"github.com/hashicorp/go-oracle-terraform/compute"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceOPCIPAssociation() *schema.Resource {
return &schema.Resource{
Create: resourceOPCIPAssociationCreate,
Read: resourceOPCIPAssociationRead,
Delete: resourceOPCIPAssociationDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"vcable": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"parent_pool": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}
func resourceOPCIPAssociationCreate(d *schema.ResourceData, meta interface{}) error {
vCable := d.Get("vcable").(string)
parentPool := d.Get("parent_pool").(string)
client := meta.(*OPCClient).computeClient.IPAssociations()
input := compute.CreateIPAssociationInput{
ParentPool: parentPool,
VCable: vCable,
}
info, err := client.CreateIPAssociation(&input)
if err != nil {
return fmt.Errorf("Error creating ip association between vcable %s and parent pool %s: %s", vCable, parentPool, err)
}
d.SetId(info.Name)
return resourceOPCIPAssociationRead(d, meta)
}
func resourceOPCIPAssociationRead(d *schema.ResourceData, meta interface{}) error {
computeClient := meta.(*OPCClient).computeClient.IPAssociations()
name := d.Id()
input := compute.GetIPAssociationInput{
Name: name,
}
result, err := computeClient.GetIPAssociation(&input)
if err != nil {
// IP Association does not exist
if client.WasNotFoundError(err) {
d.SetId("")
return nil
}
return fmt.Errorf("Error reading ip association '%s': %s", name, err)
}
if result == nil {
d.SetId("")
return nil
}
d.Set("name", result.Name)
d.Set("parent_pool", result.ParentPool)
d.Set("vcable", result.VCable)
return nil
}
func resourceOPCIPAssociationDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*OPCClient).computeClient.IPAssociations()
name := d.Id()
input := compute.DeleteIPAssociationInput{
Name: name,
}
if err := client.DeleteIPAssociation(&input); err != nil {
return fmt.Errorf("Error deleting ip association '%s': %s", name, err)
}
return nil
}