Skip to content

Commit

Permalink
Updating iothub endpoints with latest provider features
Browse files Browse the repository at this point in the history
  • Loading branch information
mbfrahry committed Nov 18, 2019
1 parent 68fce8c commit eae9588
Show file tree
Hide file tree
Showing 14 changed files with 198 additions and 146 deletions.
8 changes: 3 additions & 5 deletions azurerm/resource_arm_iothub.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,9 @@ func resourceArmIotHub() *schema.Resource {
},

"endpoint": {
Type: schema.TypeList,
Optional: true,
Computed: true,
Deprecated: "Use one of the `azurerm_iothub_endpoint_storage_container`, `azurerm_iothub_endpoint_eventhub`, `azurerm_iothub_endpoint_servicebus_queue`, `azurerm_iothub_endpoint_servicebus_topic` resources instead.",
Type: schema.TypeList,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Expand Down Expand Up @@ -473,7 +472,6 @@ func resourceArmIotHubCreateUpdate(d *schema.ResourceData, meta interface{}) err

fallbackRoute := expandIoTHubFallbackRoute(d)
routes := expandIoTHubRoutes(d)

routingProperties := devices.RoutingProperties{
Routes: routes,
FallbackRoute: fallbackRoute,
Expand Down
62 changes: 34 additions & 28 deletions azurerm/resource_arm_iothub_endpoint_eventhub.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import (
"fmt"
"regexp"
"strings"
"time"

"github.com/Azure/azure-sdk-for-go/services/preview/iothub/mgmt/2018-12-01-preview/devices"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/locks"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

Expand All @@ -24,6 +26,13 @@ func resourceArmIotHubEndpointEventHub() *schema.Resource {
State: schema.ImportStatePassthrough,
},

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(30 * time.Minute),
Read: schema.DefaultTimeout(5 * time.Minute),
Update: schema.DefaultTimeout(30 * time.Minute),
Delete: schema.DefaultTimeout(30 * time.Minute),
},

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Expand Down Expand Up @@ -60,8 +69,8 @@ func resourceArmIotHubEndpointEventHub() *schema.Resource {

func resourceArmIotHubEndpointEventHubCreateUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).IoTHub.ResourceClient
ctx := meta.(*ArmClient).StopContext
subscriptionID := meta.(*ArmClient).subscriptionId
ctx, cancel := timeouts.ForCreateUpdate(meta.(*ArmClient).StopContext, d)
defer cancel()

iothubName := d.Get("iothub_name").(string)
resourceGroup := d.Get("resource_group_name").(string)
Expand All @@ -79,20 +88,16 @@ func resourceArmIotHubEndpointEventHubCreateUpdate(d *schema.ResourceData, meta
}

endpointName := d.Get("name").(string)

resourceId := fmt.Sprintf("%s/Endpoints/%s", *iothub.ID, endpointName)

connectionStr := d.Get("connection_string").(string)

eventhubEndpoint := devices.RoutingEventHubProperties{
ConnectionString: &connectionStr,
Name: &endpointName,
SubscriptionID: &subscriptionID,
ResourceGroup: &resourceGroup,
ConnectionString: utils.String(d.Get("connection_string").(string)),
Name: utils.String(endpointName),
SubscriptionID: utils.String(meta.(*ArmClient).subscriptionId),
ResourceGroup: utils.String(resourceGroup),
}

routing := iothub.Properties.Routing

if routing == nil {
routing = &devices.RoutingProperties{}
}
Expand All @@ -110,13 +115,14 @@ func resourceArmIotHubEndpointEventHubCreateUpdate(d *schema.ResourceData, meta

alreadyExists := false
for _, existingEndpoint := range *routing.Endpoints.EventHubs {
if strings.EqualFold(*existingEndpoint.Name, endpointName) {
if d.IsNewResource() && requireResourcesToBeImported {
return tf.ImportAsExistsError("azurerm_iothub_endpoint_eventhub", resourceId)
if existingEndpointName := existingEndpoint.Name; existingEndpointName != nil {
if strings.EqualFold(*existingEndpointName, endpointName) {
if d.IsNewResource() && requireResourcesToBeImported {
return tf.ImportAsExistsError("azurerm_iothub_endpoint_eventhub", resourceId)
}
endpoints = append(endpoints, eventhubEndpoint)
alreadyExists = true
}
endpoints = append(endpoints, eventhubEndpoint)
alreadyExists = true

} else {
endpoints = append(endpoints, existingEndpoint)
}
Expand All @@ -127,7 +133,6 @@ func resourceArmIotHubEndpointEventHubCreateUpdate(d *schema.ResourceData, meta
} else if !alreadyExists {
return fmt.Errorf("Unable to find EventHub Endpoint %q defined for IotHub %q (Resource Group %q)", endpointName, iothubName, resourceGroup)
}

routing.Endpoints.EventHubs = &endpoints

future, err := client.CreateOrUpdate(ctx, resourceGroup, iothubName, iothub, "")
Expand All @@ -140,16 +145,15 @@ func resourceArmIotHubEndpointEventHubCreateUpdate(d *schema.ResourceData, meta
}

d.SetId(resourceId)

return resourceArmIotHubEndpointEventHubRead(d, meta)
}

func resourceArmIotHubEndpointEventHubRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).IoTHub.ResourceClient
ctx := meta.(*ArmClient).StopContext
ctx, cancel := timeouts.ForRead(meta.(*ArmClient).StopContext, d)
defer cancel()

parsedIothubEndpointId, err := parseAzureResourceID(d.Id())

if err != nil {
return err
}
Expand All @@ -173,8 +177,10 @@ func resourceArmIotHubEndpointEventHubRead(d *schema.ResourceData, meta interfac

if endpoints := iothub.Properties.Routing.Endpoints.EventHubs; endpoints != nil {
for _, endpoint := range *endpoints {
if strings.EqualFold(*endpoint.Name, endpointName) {
d.Set("connection_string", endpoint.ConnectionString)
if existingEndpointName := endpoint.Name; existingEndpointName != nil {
if strings.EqualFold(*existingEndpointName, endpointName) {
d.Set("connection_string", endpoint.ConnectionString)
}
}
}
}
Expand All @@ -184,10 +190,10 @@ func resourceArmIotHubEndpointEventHubRead(d *schema.ResourceData, meta interfac

func resourceArmIotHubEndpointEventHubDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).IoTHub.ResourceClient
ctx := meta.(*ArmClient).StopContext
ctx, cancel := timeouts.ForDelete(meta.(*ArmClient).StopContext, d)
defer cancel()

parsedIothubEndpointId, err := parseAzureResourceID(d.Id())

if err != nil {
return err
}
Expand All @@ -204,7 +210,6 @@ func resourceArmIotHubEndpointEventHubDelete(d *schema.ResourceData, meta interf
if utils.ResponseWasNotFound(iothub.Response) {
return fmt.Errorf("IotHub %q (Resource Group %q) was not found", iothubName, resourceGroup)
}

return fmt.Errorf("Error loading IotHub %q (Resource Group %q): %+v", iothubName, resourceGroup, err)
}

Expand All @@ -219,11 +224,12 @@ func resourceArmIotHubEndpointEventHubDelete(d *schema.ResourceData, meta interf

updatedEndpoints := make([]devices.RoutingEventHubProperties, 0)
for _, endpoint := range *endpoints {
if !strings.EqualFold(*endpoint.Name, endpointName) {
updatedEndpoints = append(updatedEndpoints, endpoint)
if existingEndpointName := endpoint.Name; existingEndpointName != nil {
if !strings.EqualFold(*existingEndpointName, endpointName) {
updatedEndpoints = append(updatedEndpoints, endpoint)
}
}
}

iothub.Properties.Routing.Endpoints.EventHubs = &updatedEndpoints

future, err := client.CreateOrUpdate(ctx, resourceGroup, iothubName, iothub, "")
Expand Down
11 changes: 5 additions & 6 deletions azurerm/resource_arm_iothub_endpoint_eventhub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ func TestAccAzureRMIotHubEndpointEventHub_requiresImport(t *testing.T) {
t.Skip("Skipping since resources aren't required to be imported")
return
}

resourceName := "azurerm_iothub_endpoint_eventhub.test"
rInt := tf.AccRandTimeInt()
location := testLocation()
Expand All @@ -68,7 +67,7 @@ func TestAccAzureRMIotHubEndpointEventHub_requiresImport(t *testing.T) {
func testAccAzureRMIotHubEndpointEventHub_basic(rInt int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%[1]d"
name = "acctestRG-iothub-%[1]d"
location = "%[2]s"
}
Expand Down Expand Up @@ -182,7 +181,6 @@ func testAccAzureRMIotHubEndpointEventHubExists(resourceName string) resource.Te
}

return fmt.Errorf("Bad: No EventHub endpoint %s defined for IotHub %s", endpointName, iothubName)

}
}

Expand All @@ -194,7 +192,6 @@ func testAccAzureRMIotHubEndpointEventHubDestroy(s *terraform.State) error {
if rs.Type != "azurerm_iothub_endpoint_eventhub" {
continue
}

endpointName := rs.Primary.Attributes["name"]
iothubName := rs.Primary.Attributes["iothub_name"]
resourceGroup := rs.Primary.Attributes["resource_group_name"]
Expand All @@ -217,8 +214,10 @@ func testAccAzureRMIotHubEndpointEventHubDestroy(s *terraform.State) error {
}

for _, endpoint := range *endpoints {
if strings.EqualFold(*endpoint.Name, endpointName) {
return fmt.Errorf("Bad: EventHub endpoint %s still exists on IoTHb %s", endpointName, iothubName)
if existingEndpointName := endpoint.Name; existingEndpointName != nil {
if strings.EqualFold(*existingEndpointName, endpointName) {
return fmt.Errorf("Bad: EventHub endpoint %s still exists on IoTHb %s", endpointName, iothubName)
}
}
}
}
Expand Down
59 changes: 35 additions & 24 deletions azurerm/resource_arm_iothub_endpoint_servicebus_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ package azurerm

import (
"fmt"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/locks"
"regexp"
"strings"
"time"

"github.com/Azure/azure-sdk-for-go/services/preview/iothub/mgmt/2018-12-01-preview/devices"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/locks"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

Expand All @@ -24,6 +26,13 @@ func resourceArmIotHubEndpointServiceBusQueue() *schema.Resource {
State: schema.ImportStatePassthrough,
},

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(30 * time.Minute),
Read: schema.DefaultTimeout(5 * time.Minute),
Update: schema.DefaultTimeout(30 * time.Minute),
Delete: schema.DefaultTimeout(30 * time.Minute),
},

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Expand Down Expand Up @@ -60,7 +69,8 @@ func resourceArmIotHubEndpointServiceBusQueue() *schema.Resource {

func resourceArmIotHubEndpointServiceBusQueueCreateUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).IoTHub.ResourceClient
ctx := meta.(*ArmClient).StopContext
ctx, cancel := timeouts.ForCreateUpdate(meta.(*ArmClient).StopContext, d)
defer cancel()
subscriptionID := meta.(*ArmClient).subscriptionId

iothubName := d.Get("iothub_name").(string)
Expand All @@ -79,20 +89,16 @@ func resourceArmIotHubEndpointServiceBusQueueCreateUpdate(d *schema.ResourceData
}

endpointName := d.Get("name").(string)

resourceId := fmt.Sprintf("%s/Endpoints/%s", *iothub.ID, endpointName)

connectionStr := d.Get("connection_string").(string)

queueEndpoint := devices.RoutingServiceBusQueueEndpointProperties{
ConnectionString: &connectionStr,
Name: &endpointName,
SubscriptionID: &subscriptionID,
ResourceGroup: &resourceGroup,
ConnectionString: utils.String(d.Get("connection_string").(string)),
Name: utils.String(endpointName),
SubscriptionID: utils.String(subscriptionID),
ResourceGroup: utils.String(resourceGroup),
}

routing := iothub.Properties.Routing

if routing == nil {
routing = &devices.RoutingProperties{}
}
Expand All @@ -105,18 +111,18 @@ func resourceArmIotHubEndpointServiceBusQueueCreateUpdate(d *schema.ResourceData
queues := make([]devices.RoutingServiceBusQueueEndpointProperties, 0)
routing.Endpoints.ServiceBusQueues = &queues
}

endpoints := make([]devices.RoutingServiceBusQueueEndpointProperties, 0)

alreadyExists := false
for _, existingEndpoint := range *routing.Endpoints.ServiceBusQueues {
if strings.EqualFold(*existingEndpoint.Name, endpointName) {
if d.IsNewResource() && requireResourcesToBeImported {
return tf.ImportAsExistsError("azurerm_iothub_endpoint_servicebus_queue", resourceId)
if existingEndpointName := existingEndpoint.Name; existingEndpointName != nil {
if strings.EqualFold(*existingEndpointName, endpointName) {
if d.IsNewResource() && requireResourcesToBeImported {
return tf.ImportAsExistsError("azurerm_iothub_endpoint_servicebus_queue", resourceId)
}
endpoints = append(endpoints, queueEndpoint)
alreadyExists = true
}
endpoints = append(endpoints, queueEndpoint)
alreadyExists = true

} else {
endpoints = append(endpoints, existingEndpoint)
}
Expand All @@ -127,7 +133,6 @@ func resourceArmIotHubEndpointServiceBusQueueCreateUpdate(d *schema.ResourceData
} else if !alreadyExists {
return fmt.Errorf("Unable to find ServiceBus Queue Endpoint %q defined for IotHub %q (Resource Group %q)", endpointName, iothubName, resourceGroup)
}

routing.Endpoints.ServiceBusQueues = &endpoints

future, err := client.CreateOrUpdate(ctx, resourceGroup, iothubName, iothub, "")
Expand All @@ -146,7 +151,8 @@ func resourceArmIotHubEndpointServiceBusQueueCreateUpdate(d *schema.ResourceData

func resourceArmIotHubEndpointServiceBusQueueRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).IoTHub.ResourceClient
ctx := meta.(*ArmClient).StopContext
ctx, cancel := timeouts.ForRead(meta.(*ArmClient).StopContext, d)
defer cancel()

parsedIothubEndpointId, err := parseAzureResourceID(d.Id())

Expand All @@ -173,8 +179,10 @@ func resourceArmIotHubEndpointServiceBusQueueRead(d *schema.ResourceData, meta i

if endpoints := iothub.Properties.Routing.Endpoints.ServiceBusQueues; endpoints != nil {
for _, endpoint := range *endpoints {
if strings.EqualFold(*endpoint.Name, endpointName) {
d.Set("connection_string", endpoint.ConnectionString)
if existingEndpointName := endpoint.Name; existingEndpointName != nil {
if strings.EqualFold(*existingEndpointName, endpointName) {
d.Set("connection_string", endpoint.ConnectionString)
}
}
}
}
Expand All @@ -184,7 +192,8 @@ func resourceArmIotHubEndpointServiceBusQueueRead(d *schema.ResourceData, meta i

func resourceArmIotHubEndpointServiceBusQueueDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).IoTHub.ResourceClient
ctx := meta.(*ArmClient).StopContext
ctx, cancel := timeouts.ForDelete(meta.(*ArmClient).StopContext, d)
defer cancel()

parsedIothubEndpointId, err := parseAzureResourceID(d.Id())

Expand Down Expand Up @@ -219,8 +228,10 @@ func resourceArmIotHubEndpointServiceBusQueueDelete(d *schema.ResourceData, meta

updatedEndpoints := make([]devices.RoutingServiceBusQueueEndpointProperties, 0)
for _, endpoint := range *endpoints {
if !strings.EqualFold(*endpoint.Name, endpointName) {
updatedEndpoints = append(updatedEndpoints, endpoint)
if existingEndpointName := endpoint.Name; existingEndpointName != nil {
if !strings.EqualFold(*existingEndpointName, endpointName) {
updatedEndpoints = append(updatedEndpoints, endpoint)
}
}
}

Expand Down
Loading

0 comments on commit eae9588

Please sign in to comment.