-
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.
New Resource:
azurerm_iothub_consumer_group
(#2243)
* iothub: refactoring * New Resource: `azurerm_iothub_consumer_group` Tests pass: ``` $ acctests azurerm TestAccAzureRMIotHubConsumerGroup_ === RUN TestAccAzureRMIotHubConsumerGroup_events --- PASS: TestAccAzureRMIotHubConsumerGroup_events (280.43s) === RUN TestAccAzureRMIotHubConsumerGroup_operationsMonitoringEvents --- PASS: TestAccAzureRMIotHubConsumerGroup_operationsMonitoringEvents (211.24s) PASS ok github.com/terraform-providers/terraform-provider-azurerm/azurerm 493.476s ``` Fixes #2230 * adding validation for iothub names Tests pass: ``` === RUN TestValidateIoTHubName --- PASS: TestValidateIoTHubName (0.00s) === RUN TestValidateIoTHubConsumerGroupName --- PASS: TestValidateIoTHubConsumerGroupName (0.00s) PASS ``` * fixing the alignment
- Loading branch information
1 parent
0cfb7a8
commit aaaf121
Showing
9 changed files
with
475 additions
and
41 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package validate | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
) | ||
|
||
func IoTHubName(v interface{}, k string) (ws []string, es []error) { | ||
value := v.(string) | ||
|
||
// Portal: The value must contain only alphanumeric characters or the following: - | ||
if matched := regexp.MustCompile(`^[0-9a-zA-Z-]{1,}$`).Match([]byte(value)); !matched { | ||
es = append(es, fmt.Errorf("%q may only contain alphanumeric characters and dashes", k)) | ||
} | ||
|
||
return | ||
} | ||
|
||
func IoTHubConsumerGroupName(v interface{}, k string) (ws []string, es []error) { | ||
value := v.(string) | ||
|
||
// Portal: The value must contain only alphanumeric characters or the following: - . _ | ||
if matched := regexp.MustCompile(`^[0-9a-zA-Z-._]{1,}$`).Match([]byte(value)); !matched { | ||
es = append(es, fmt.Errorf("%q may only contain alphanumeric characters and dashes, periods and underscores", k)) | ||
} | ||
|
||
return | ||
} |
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,63 @@ | ||
package validate | ||
|
||
import "testing" | ||
|
||
func TestValidateIoTHubName(t *testing.T) { | ||
validNames := []string{ | ||
"valid-name", | ||
"valid02-name", | ||
"validName1", | ||
"-validname1", | ||
"double-hyphen--valid", | ||
} | ||
for _, v := range validNames { | ||
_, errors := IoTHubName(v, "example") | ||
if len(errors) != 0 { | ||
t.Fatalf("%q should be a valid IoT Hub Name: %q", v, errors) | ||
} | ||
} | ||
|
||
invalidNames := []string{ | ||
"", | ||
"invalid_name", | ||
"invalid!", | ||
"!@£", | ||
"hello.world", | ||
} | ||
for _, v := range invalidNames { | ||
_, errors := IoTHubName(v, "name") | ||
if len(errors) == 0 { | ||
t.Fatalf("%q should be an invalid IoT Hub Name", v) | ||
} | ||
} | ||
} | ||
|
||
func TestValidateIoTHubConsumerGroupName(t *testing.T) { | ||
validNames := []string{ | ||
"valid-name", | ||
"valid02-name", | ||
"validName1", | ||
"-validname1", | ||
"valid_name", | ||
"double-hyphen--valid", | ||
"hello.world", | ||
} | ||
for _, v := range validNames { | ||
_, errors := IoTHubConsumerGroupName(v, "example") | ||
if len(errors) != 0 { | ||
t.Fatalf("%q should be a valid IoT Hub Consumer Group Name: %q", v, errors) | ||
} | ||
} | ||
|
||
invalidNames := []string{ | ||
"", | ||
"invalid!", | ||
"!@£", | ||
} | ||
for _, v := range invalidNames { | ||
_, errors := IoTHubConsumerGroupName(v, "name") | ||
if len(errors) == 0 { | ||
t.Fatalf("%q should be an invalid IoT Hub Consumer Group Name", v) | ||
} | ||
} | ||
} |
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,128 @@ | ||
package azurerm | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/hashicorp/terraform/helper/schema" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" | ||
) | ||
|
||
func resourceArmIotHubConsumerGroup() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceArmIotHubConsumerGroupCreate, | ||
Read: resourceArmIotHubConsumerGroupRead, | ||
Delete: resourceArmIotHubConsumerGroupDelete, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validate.IoTHubConsumerGroupName, | ||
}, | ||
|
||
"iothub_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validate.IoTHubName, | ||
}, | ||
|
||
"eventhub_endpoint_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
|
||
"resource_group_name": resourceGroupNameSchema(), | ||
}, | ||
} | ||
} | ||
|
||
func resourceArmIotHubConsumerGroupCreate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ArmClient).iothubResourceClient | ||
ctx := meta.(*ArmClient).StopContext | ||
log.Printf("[INFO] preparing arguments for AzureRM IoTHub Consumer Group creation.") | ||
|
||
name := d.Get("name").(string) | ||
iotHubName := d.Get("iothub_name").(string) | ||
endpointName := d.Get("eventhub_endpoint_name").(string) | ||
resourceGroup := d.Get("resource_group_name").(string) | ||
|
||
_, err := client.CreateEventHubConsumerGroup(ctx, resourceGroup, iotHubName, endpointName, name) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
read, err := client.GetEventHubConsumerGroup(ctx, resourceGroup, iotHubName, endpointName, name) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if read.ID == nil { | ||
return fmt.Errorf("Cannot read IoTHub Consumer Group %q (Resource Group %q) ID", name, resourceGroup) | ||
} | ||
|
||
d.SetId(*read.ID) | ||
|
||
return resourceArmIotHubConsumerGroupRead(d, meta) | ||
} | ||
|
||
func resourceArmIotHubConsumerGroupRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ArmClient).iothubResourceClient | ||
ctx := meta.(*ArmClient).StopContext | ||
|
||
id, err := parseAzureResourceID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
resourceGroup := id.ResourceGroup | ||
iotHubName := id.Path["IotHubs"] | ||
endpointName := id.Path["eventHubEndpoints"] | ||
name := id.Path["ConsumerGroups"] | ||
|
||
resp, err := client.GetEventHubConsumerGroup(ctx, resourceGroup, iotHubName, endpointName, name) | ||
if err != nil { | ||
if utils.ResponseWasNotFound(resp.Response) { | ||
d.SetId("") | ||
return nil | ||
} | ||
return fmt.Errorf("Error making Read request on IoTHub Consumer Group %s: %+v", name, err) | ||
} | ||
|
||
d.Set("name", name) | ||
d.Set("iothub_name", iotHubName) | ||
d.Set("eventhub_endpoint_name", endpointName) | ||
d.Set("resource_group_name", resourceGroup) | ||
|
||
return nil | ||
} | ||
|
||
func resourceArmIotHubConsumerGroupDelete(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ArmClient).iothubResourceClient | ||
ctx := meta.(*ArmClient).StopContext | ||
|
||
id, err := parseAzureResourceID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
resourceGroup := id.ResourceGroup | ||
iotHubName := id.Path["IotHubs"] | ||
endpointName := id.Path["eventHubEndpoints"] | ||
name := id.Path["ConsumerGroups"] | ||
|
||
resp, err := client.DeleteEventHubConsumerGroup(ctx, resourceGroup, iotHubName, endpointName, name) | ||
|
||
if err != nil { | ||
if !utils.ResponseWasNotFound(resp) { | ||
return fmt.Errorf("Error issuing delete request for IoTHub Consumer Group %q (Resource Group %q): %+v", name, resourceGroup, err) | ||
} | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.