-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
2,377 additions
and
56 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package validate | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
) | ||
|
||
func StorageContainerName(v interface{}, k string) (warnings []string, errors []error) { | ||
value := v.(string) | ||
|
||
if !regexp.MustCompile(`^\$root$|^\$web$|^[0-9a-z-]+$`).MatchString(value) { | ||
errors = append(errors, fmt.Errorf( | ||
"only lowercase alphanumeric characters and hyphens allowed in %q: %q", | ||
k, value)) | ||
} | ||
if len(value) < 3 || len(value) > 63 { | ||
errors = append(errors, fmt.Errorf( | ||
"%q must be between 3 and 63 characters: %q", k, value)) | ||
} | ||
if regexp.MustCompile(`^-`).MatchString(value) { | ||
errors = append(errors, fmt.Errorf( | ||
"%q cannot begin with a hyphen: %q", k, value)) | ||
} | ||
return warnings, errors | ||
} |
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,239 @@ | ||
package azurerm | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
|
||
"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/utils" | ||
) | ||
|
||
func resourceArmIotHubEndpointEventHub() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceArmIotHubEndpointEventHubCreateUpdate, | ||
Read: resourceArmIotHubEndpointEventHubRead, | ||
Update: resourceArmIotHubEndpointEventHubCreateUpdate, | ||
Delete: resourceArmIotHubEndpointEventHubDelete, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validate.IoTHubEndpointName, | ||
}, | ||
|
||
"resource_group_name": azure.SchemaResourceGroupName(), | ||
|
||
"iothub_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validate.IoTHubName, | ||
}, | ||
|
||
"connection_string": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool { | ||
sharedAccessKeyRegex := regexp.MustCompile("SharedAccessKey=[^;]+") | ||
sbProtocolRegex := regexp.MustCompile("sb://([^:]+)(:5671)?/;") | ||
|
||
maskedNew := sbProtocolRegex.ReplaceAllString(new, "sb://$1:5671/;") | ||
maskedNew = sharedAccessKeyRegex.ReplaceAllString(maskedNew, "SharedAccessKey=****") | ||
return (new == d.Get(k).(string)) && (maskedNew == old) | ||
}, | ||
Sensitive: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceArmIotHubEndpointEventHubCreateUpdate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ArmClient).IoTHub.ResourceClient | ||
ctx := meta.(*ArmClient).StopContext | ||
subscriptionID := meta.(*ArmClient).subscriptionId | ||
|
||
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) | ||
} | ||
|
||
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, | ||
} | ||
|
||
routing := iothub.Properties.Routing | ||
|
||
if routing == nil { | ||
routing = &devices.RoutingProperties{} | ||
} | ||
|
||
if routing.Endpoints == nil { | ||
routing.Endpoints = &devices.RoutingEndpoints{} | ||
} | ||
|
||
if routing.Endpoints.EventHubs == nil { | ||
eventHubs := make([]devices.RoutingEventHubProperties, 0) | ||
routing.Endpoints.EventHubs = &eventHubs | ||
} | ||
|
||
endpoints := make([]devices.RoutingEventHubProperties, 0) | ||
|
||
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) | ||
} | ||
endpoints = append(endpoints, eventhubEndpoint) | ||
alreadyExists = true | ||
|
||
} else { | ||
endpoints = append(endpoints, existingEndpoint) | ||
} | ||
} | ||
|
||
if d.IsNewResource() { | ||
endpoints = append(endpoints, eventhubEndpoint) | ||
} 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, "") | ||
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 resourceArmIotHubEndpointEventHubRead(d, meta) | ||
} | ||
|
||
func resourceArmIotHubEndpointEventHubRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ArmClient).IoTHub.ResourceClient | ||
ctx := meta.(*ArmClient).StopContext | ||
|
||
parsedIothubEndpointId, err := parseAzureResourceID(d.Id()) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
resourceGroup := parsedIothubEndpointId.ResourceGroup | ||
iothubName := parsedIothubEndpointId.Path["IotHubs"] | ||
endpointName := parsedIothubEndpointId.Path["Endpoints"] | ||
|
||
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("name", endpointName) | ||
d.Set("iothub_name", iothubName) | ||
d.Set("resource_group_name", resourceGroup) | ||
|
||
if iothub.Properties == nil || iothub.Properties.Routing == nil || iothub.Properties.Routing.Endpoints == nil { | ||
return nil | ||
} | ||
|
||
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) | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceArmIotHubEndpointEventHubDelete(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ArmClient).IoTHub.ResourceClient | ||
ctx := meta.(*ArmClient).StopContext | ||
|
||
parsedIothubEndpointId, err := parseAzureResourceID(d.Id()) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
resourceGroup := parsedIothubEndpointId.ResourceGroup | ||
iothubName := parsedIothubEndpointId.Path["IotHubs"] | ||
endpointName := parsedIothubEndpointId.Path["Endpoints"] | ||
|
||
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.Endpoints == nil { | ||
return nil | ||
} | ||
endpoints := iothub.Properties.Routing.Endpoints.EventHubs | ||
|
||
if endpoints == nil { | ||
return nil | ||
} | ||
|
||
updatedEndpoints := make([]devices.RoutingEventHubProperties, 0) | ||
for _, endpoint := range *endpoints { | ||
if !strings.EqualFold(*endpoint.Name, endpointName) { | ||
updatedEndpoints = append(updatedEndpoints, endpoint) | ||
} | ||
} | ||
|
||
iothub.Properties.Routing.Endpoints.EventHubs = &updatedEndpoints | ||
|
||
future, err := client.CreateOrUpdate(ctx, resourceGroup, iothubName, iothub, "") | ||
if err != nil { | ||
return fmt.Errorf("Error updating IotHub %q (Resource Group %q) with EventHub Endpoint %q: %+v", iothubName, resourceGroup, endpointName, err) | ||
} | ||
|
||
if err = future.WaitForCompletionRef(ctx, client.Client); err != nil { | ||
return fmt.Errorf("Error waiting for IotHub %q (Resource Group %q) to finish updating EventHub Endpoint %q: %+v", iothubName, resourceGroup, endpointName, err) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.