-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
resource_arm_eventhub.go
167 lines (134 loc) · 4.21 KB
/
resource_arm_eventhub.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package azurerm
import (
"fmt"
"log"
"net/http"
"github.com/Azure/azure-sdk-for-go/arm/eventhub"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
func resourceArmEventHub() *schema.Resource {
return &schema.Resource{
Create: resourceArmEventHubCreate,
Read: resourceArmEventHubRead,
Update: resourceArmEventHubCreate,
Delete: resourceArmEventHubDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"namespace_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"resource_group_name": resourceGroupNameSchema(),
// TODO: remove me in the next major version
"location": deprecatedLocationSchema(),
"partition_count": {
Type: schema.TypeInt,
Required: true,
ValidateFunc: validateEventHubPartitionCount,
},
"message_retention": {
Type: schema.TypeInt,
Required: true,
ValidateFunc: validateEventHubMessageRetentionCount,
},
"partition_ids": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
Computed: true,
},
},
}
}
func resourceArmEventHubCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient)
eventhubClient := client.eventHubClient
log.Printf("[INFO] preparing arguments for Azure ARM EventHub creation.")
name := d.Get("name").(string)
namespaceName := d.Get("namespace_name").(string)
resGroup := d.Get("resource_group_name").(string)
partitionCount := int64(d.Get("partition_count").(int))
messageRetention := int64(d.Get("message_retention").(int))
parameters := eventhub.Model{
Properties: &eventhub.Properties{
PartitionCount: &partitionCount,
MessageRetentionInDays: &messageRetention,
},
}
_, err := eventhubClient.CreateOrUpdate(resGroup, namespaceName, name, parameters)
if err != nil {
return err
}
read, err := eventhubClient.Get(resGroup, namespaceName, name)
if err != nil {
return err
}
if read.ID == nil {
return fmt.Errorf("Cannot read EventHub %s (resource group %s) ID", name, resGroup)
}
d.SetId(*read.ID)
return resourceArmEventHubRead(d, meta)
}
func resourceArmEventHubRead(d *schema.ResourceData, meta interface{}) error {
eventhubClient := meta.(*ArmClient).eventHubClient
id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resGroup := id.ResourceGroup
namespaceName := id.Path["namespaces"]
name := id.Path["eventhubs"]
resp, err := eventhubClient.Get(resGroup, namespaceName, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
d.SetId("")
return nil
}
return fmt.Errorf("Error making Read request on Azure EventHub %s: %s", name, err)
}
d.Set("name", resp.Name)
d.Set("namespace_name", namespaceName)
d.Set("resource_group_name", resGroup)
d.Set("partition_count", resp.Properties.PartitionCount)
d.Set("message_retention", resp.Properties.MessageRetentionInDays)
d.Set("partition_ids", resp.Properties.PartitionIds)
return nil
}
func resourceArmEventHubDelete(d *schema.ResourceData, meta interface{}) error {
eventhubClient := meta.(*ArmClient).eventHubClient
id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resGroup := id.ResourceGroup
namespaceName := id.Path["namespaces"]
name := id.Path["eventhubs"]
resp, err := eventhubClient.Delete(resGroup, namespaceName, name)
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("Error issuing Azure ARM delete request of EventHub'%s': %s", name, err)
}
return nil
}
func validateEventHubPartitionCount(v interface{}, k string) (ws []string, errors []error) {
value := v.(int)
if !(32 >= value && value >= 2) {
errors = append(errors, fmt.Errorf("EventHub Partition Count has to be between 2 and 32"))
}
return
}
func validateEventHubMessageRetentionCount(v interface{}, k string) (ws []string, errors []error) {
value := v.(int)
if !(7 >= value && value >= 1) {
errors = append(errors, fmt.Errorf("EventHub Retention Count has to be between 1 and 7"))
}
return
}