-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New Resource: azurerm_iothub_fallback_route
#4965
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
deb7b59
iot_fallback_route resource
3dd6c6d
Finish iothub fallback route
mbfrahry 87f0d02
Add missing subcategory in docs
mbfrahry 4978609
goimports
mbfrahry 3cbeac7
goimports
mbfrahry dab62ff
Adding additional validation
mbfrahry File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
package azurerm | ||
|
||
import ( | ||
"fmt" | ||
|
||
"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" | ||
) | ||
|
||
func resourceArmIotHubFallbackRoute() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceArmIotHubFallbackRouteCreateUpdate, | ||
Read: resourceArmIotHubFallbackRouteRead, | ||
Update: resourceArmIotHubFallbackRouteCreateUpdate, | ||
Delete: resourceArmIotHubFallbackRouteDelete, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"resource_group_name": azure.SchemaResourceGroupName(), | ||
|
||
"iothub_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validate.IoTHubName, | ||
}, | ||
|
||
"condition": { | ||
// The condition is a string value representing device-to-cloud message routes query expression | ||
// https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-query-language#device-to-cloud-message-routes-query-expressions | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Default: "true", | ||
}, | ||
|
||
"endpoint_names": { | ||
Type: schema.TypeList, | ||
Required: true, | ||
// Currently only one endpoint is allowed. With that comment from Microsoft, we'll leave this open to enhancement when they add multiple endpoint support. | ||
MaxItems: 1, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
ValidateFunc: validate.NoEmptyStrings, | ||
}, | ||
}, | ||
|
||
"enabled": { | ||
Type: schema.TypeBool, | ||
Required: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceArmIotHubFallbackRouteCreateUpdate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ArmClient).IoTHub.ResourceClient | ||
ctx, cancel := timeouts.ForCreateUpdate(meta.(*ArmClient).StopContext, d) | ||
defer cancel() | ||
|
||
iothubName := d.Get("iothub_name").(string) | ||
resourceGroup := d.Get("resource_group_name").(string) | ||
|
||
locks.ByName(iothubName, iothubResourceName) | ||
defer locks.UnlockByName(iothubName, iothubResourceName) | ||
|
||
iothub, err := client.Get(ctx, resourceGroup, iothubName) | ||
if err != nil { | ||
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) | ||
} | ||
|
||
routing := iothub.Properties.Routing | ||
|
||
if routing == nil { | ||
routing = &devices.RoutingProperties{} | ||
} | ||
|
||
resourceId := fmt.Sprintf("%s/FallbackRoute/defined", *iothub.ID) | ||
if d.IsNewResource() && routing.FallbackRoute != nil && requireResourcesToBeImported { | ||
return tf.ImportAsExistsError("azurerm_iothub_fallback_route", resourceId) | ||
} | ||
|
||
routing.FallbackRoute = &devices.FallbackRouteProperties{ | ||
Source: utils.String(string(devices.RoutingSourceDeviceMessages)), | ||
Condition: utils.String(d.Get("condition").(string)), | ||
EndpointNames: utils.ExpandStringSlice(d.Get("endpoint_names").([]interface{})), | ||
IsEnabled: utils.Bool(d.Get("enabled").(bool)), | ||
} | ||
|
||
future, err := client.CreateOrUpdate(ctx, resourceGroup, iothubName, iothub, "") | ||
if err != nil { | ||
return fmt.Errorf("Error creating/updating IotHub %q (Resource Group %q): %+v", iothubName, resourceGroup, err) | ||
} | ||
|
||
if err = future.WaitForCompletionRef(ctx, client.Client); err != nil { | ||
return fmt.Errorf("Error waiting for the completion of the creating/updating of IotHub %q (Resource Group %q): %+v", iothubName, resourceGroup, err) | ||
} | ||
|
||
d.SetId(resourceId) | ||
|
||
return resourceArmIotHubFallbackRouteRead(d, meta) | ||
} | ||
|
||
func resourceArmIotHubFallbackRouteRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ArmClient).IoTHub.ResourceClient | ||
ctx, cancel := timeouts.ForRead(meta.(*ArmClient).StopContext, d) | ||
defer cancel() | ||
|
||
parsedIothubRouteId, err := parseAzureResourceID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
resourceGroup := parsedIothubRouteId.ResourceGroup | ||
iothubName := parsedIothubRouteId.Path["IotHubs"] | ||
|
||
iothub, err := client.Get(ctx, resourceGroup, iothubName) | ||
if err != nil { | ||
return fmt.Errorf("Error loading IotHub %q (Resource Group %q): %+v", iothubName, resourceGroup, err) | ||
} | ||
|
||
d.Set("iothub_name", iothubName) | ||
d.Set("resource_group_name", resourceGroup) | ||
|
||
if props := iothub.Properties; props != nil { | ||
if routing := props.Routing; routing != nil { | ||
if fallbackRoute := routing.FallbackRoute; fallbackRoute != nil { | ||
d.Set("condition", fallbackRoute.Condition) | ||
d.Set("enabled", fallbackRoute.IsEnabled) | ||
d.Set("endpoint_names", fallbackRoute.EndpointNames) | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceArmIotHubFallbackRouteDelete(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ArmClient).IoTHub.ResourceClient | ||
ctx, cancel := timeouts.ForDelete(meta.(*ArmClient).StopContext, d) | ||
defer cancel() | ||
|
||
parsedIothubRouteId, err := parseAzureResourceID(d.Id()) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
resourceGroup := parsedIothubRouteId.ResourceGroup | ||
iothubName := parsedIothubRouteId.Path["IotHubs"] | ||
|
||
locks.ByName(iothubName, iothubResourceName) | ||
defer locks.UnlockByName(iothubName, iothubResourceName) | ||
|
||
iothub, err := client.Get(ctx, resourceGroup, iothubName) | ||
if err != nil { | ||
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) | ||
} | ||
|
||
if iothub.Properties == nil || iothub.Properties.Routing == nil || iothub.Properties.Routing.FallbackRoute == nil { | ||
return nil | ||
} | ||
|
||
iothub.Properties.Routing.FallbackRoute = nil | ||
future, err := client.CreateOrUpdate(ctx, resourceGroup, iothubName, iothub, "") | ||
if err != nil { | ||
return fmt.Errorf("Error updating IotHub %q (Resource Group %q) with Fallback Route: %+v", iothubName, resourceGroup, err) | ||
} | ||
|
||
if err = future.WaitForCompletionRef(ctx, client.Client); err != nil { | ||
return fmt.Errorf("Error waiting for IotHub %q (Resource Group %q) to finish updating Fallback Route: %+v", iothubName, resourceGroup, err) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should use
validate.IoTHubEndpointName
here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call