Skip to content
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_eventhub_namespace_authorization_rule #1572

Merged
merged 8 commits into from
Jul 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 145 additions & 0 deletions azurerm/helpers/azure/eventhub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package azure

import (
"fmt"
"log"
"regexp"

"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"

"github.com/Azure/azure-sdk-for-go/services/eventhub/mgmt/2017-04-01/eventhub"
)

//validation
func ValidateEventHubNamespaceName() schema.SchemaValidateFunc {
return validation.StringMatch(
regexp.MustCompile("^[a-zA-Z][-a-zA-Z0-9]{4,48}[a-zA-Z0-9]$"),
"The namespace name can contain only letters, numbers, and hyphens. The namespace must start with a letter, and it must end with a letter or number and be between 6 and 50 characters long.",
)
}

func ValidateEventHubName() schema.SchemaValidateFunc {
return validation.StringMatch(
regexp.MustCompile("^[a-zA-Z][-a-zA-Z0-9]{4,48}[a-zA-Z0-9]$"),
"The namespace name can contain only letters, numbers, and hyphens. The namespace must start with a letter, and it must end with a letter or number and be between 6 and 50 characters long.",
)
}

func ValidateEventHubConsumerName() schema.SchemaValidateFunc {
return validation.StringMatch(
regexp.MustCompile("^[a-zA-Z][-a-zA-Z0-9]{4,48}[a-zA-Z0-9]$"),
"The namespace name can contain only letters, numbers, and hyphens. The namespace must start with a letter, and it must end with a letter or number and be between 6 and 50 characters long.",
)
}

func ValidateEventHubAuthorizationRuleName() schema.SchemaValidateFunc {
return validation.StringMatch(
regexp.MustCompile("^[a-zA-Z0-9][-._a-zA-Z0-9]{0,48}([a-zA-Z0-9])?$"),
"The name can contain only letters, numbers, periods, hyphens and underscores. The name must start and end with a letter or number and be less the 50 characters long.",
)
}

//schema
func ExpandEventHubAuthorizationRuleRights(d *schema.ResourceData) *[]eventhub.AccessRights {
rights := []eventhub.AccessRights{}

if d.Get("listen").(bool) {
rights = append(rights, eventhub.Listen)
}

if d.Get("send").(bool) {
rights = append(rights, eventhub.Send)
}

if d.Get("manage").(bool) {
rights = append(rights, eventhub.Manage)
}

return &rights
}

func FlattenEventHubAuthorizationRuleRights(rights *[]eventhub.AccessRights) (listen bool, send bool, manage bool) {
//zero (initial) value for a bool in go is false

if rights != nil {
for _, right := range *rights {
switch right {
case eventhub.Listen:
listen = true
case eventhub.Send:
send = true
case eventhub.Manage:
manage = true
default:
log.Printf("[DEBUG] Unknown Authorization Rule Right '%s'", right)
}
}
}

return
}

func EventHubAuthorizationRuleSchemaFrom(s map[string]*schema.Schema) map[string]*schema.Schema {

authSchema := map[string]*schema.Schema{
"listen": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},

"send": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},

"manage": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},

"primary_key": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},

"primary_connection_string": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},

"secondary_key": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},

"secondary_connection_string": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},
}
return MergeSchema(s, authSchema)
}

func EventHubAuthorizationRuleCustomizeDiff(d *schema.ResourceDiff, _ interface{}) error {
listen, hasListen := d.GetOk("listen")
send, hasSend := d.GetOk("send")
manage, hasManage := d.GetOk("manage")

if !hasListen && !hasSend && !hasManage {
return fmt.Errorf("One of the `listen`, `send` or `manage` properties needs to be set")
}

if manage.(bool) && !listen.(bool) && !send.(bool) {
return fmt.Errorf("if `manage` is set both `listen` and `send` must be set to true too")
}

return nil
}
104 changes: 0 additions & 104 deletions azurerm/import_arm_eventhub_authorization_rule_test.go

This file was deleted.

1 change: 1 addition & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_eventhub_authorization_rule": resourceArmEventHubAuthorizationRule(),
"azurerm_eventhub_consumer_group": resourceArmEventHubConsumerGroup(),
"azurerm_eventhub_namespace": resourceArmEventHubNamespace(),
"azurerm_eventhub_namespace_authorization_rule": resourceArmEventHubNamespaceAuthorizationRule(),
"azurerm_express_route_circuit": resourceArmExpressRouteCircuit(),
"azurerm_express_route_circuit_authorization": resourceArmExpressRouteCircuitAuthorization(),
"azurerm_express_route_circuit_peering": resourceArmExpressRouteCircuitPeering(),
Expand Down
15 changes: 9 additions & 6 deletions azurerm/resource_arm_eventhub.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/Azure/azure-sdk-for-go/services/eventhub/mgmt/2017-04-01/eventhub"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

Expand All @@ -24,15 +25,17 @@ func resourceArmEventHub() *schema.Resource {

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: azure.ValidateEventHubName(),
},

"namespace_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: azure.ValidateEventHubNamespaceName(),
},

"resource_group_name": resourceGroupNameSchema(),
Expand Down
Loading