-
Notifications
You must be signed in to change notification settings - Fork 9.5k
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
provider/azurerm: Event Hub Namespaces #9297
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
3930906
Azure EventHub SDK
tombuildsstuff 0fb25c7
Documentation for EventHub Namespaces
tombuildsstuff c01ace0
Support for Event Hub namespaces
tombuildsstuff a610abb
Copy/Paste fail
tombuildsstuff 63bd996
Fixing a casing issue
tombuildsstuff d1aaad5
Adding some additional test cases to account for the Azure config
tombuildsstuff fe5c99f
Fixing the casing of the SKU
tombuildsstuff 369ac47
Allowing changes between SKU's
tombuildsstuff c20075b
Casing on the SKU
tombuildsstuff e7bf431
Removing a default variable
tombuildsstuff b0e9c78
Adding an import test
tombuildsstuff 81c11c0
Mapping the missing Location/Resource Group Name properties
tombuildsstuff 4171f39
Registering the correct provider again..
tombuildsstuff c4963eb
Removing the duplicate
tombuildsstuff 5d7e768
Registering the Microsoft.EventHub provider
tombuildsstuff 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
33 changes: 33 additions & 0 deletions
33
builtin/providers/azurerm/import_arm_eventhub_namespace_test.go
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,33 @@ | ||
package azurerm | ||
|
||
import ( | ||
"testing" | ||
|
||
"fmt" | ||
"github.com/hashicorp/terraform/helper/acctest" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
) | ||
|
||
func TestAccAzureRMEventHubNamespace_importBasic(t *testing.T) { | ||
resourceName := "azurerm_eventhub_namespace.test" | ||
|
||
ri := acctest.RandInt() | ||
config := fmt.Sprintf(testAccAzureRMEventHubNamespace_basic, ri, ri) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testCheckAzureRMEventHubNamespaceDestroy, | ||
Steps: []resource.TestStep{ | ||
resource.TestStep{ | ||
Config: config, | ||
}, | ||
|
||
resource.TestStep{ | ||
ResourceName: resourceName, | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
}, | ||
}) | ||
} |
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
211 changes: 211 additions & 0 deletions
211
builtin/providers/azurerm/resource_arm_eventhub_namespace.go
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,211 @@ | ||
package azurerm | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"strings" | ||
|
||
"github.com/Azure/azure-sdk-for-go/arm/eventhub" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
"net/http" | ||
) | ||
|
||
// Default Authorization Rule/Policy created by Azure, used to populate the | ||
// default connection strings and keys | ||
var eventHubNamespaceDefaultAuthorizationRule = "RootManageSharedAccessKey" | ||
|
||
func resourceArmEventHubNamespace() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceArmEventHubNamespaceCreate, | ||
Read: resourceArmEventHubNamespaceRead, | ||
Update: resourceArmEventHubNamespaceCreate, | ||
Delete: resourceArmEventHubNamespaceDelete, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
|
||
"location": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
StateFunc: azureRMNormalizeLocation, | ||
}, | ||
|
||
"resource_group_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
|
||
"sku": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validateEventHubNamespaceSku, | ||
}, | ||
|
||
"capacity": { | ||
Type: schema.TypeInt, | ||
Optional: true, | ||
Default: 1, | ||
ValidateFunc: validateEventHubNamespaceCapacity, | ||
}, | ||
|
||
"default_primary_connection_string": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"default_secondary_connection_string": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"default_primary_key": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"default_secondary_key": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"tags": tagsSchema(), | ||
}, | ||
} | ||
} | ||
|
||
func resourceArmEventHubNamespaceCreate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ArmClient) | ||
namespaceClient := client.eventHubNamespacesClient | ||
log.Printf("[INFO] preparing arguments for Azure ARM EventHub Namespace creation.") | ||
|
||
name := d.Get("name").(string) | ||
location := d.Get("location").(string) | ||
resGroup := d.Get("resource_group_name").(string) | ||
sku := d.Get("sku").(string) | ||
capacity := int32(d.Get("capacity").(int)) | ||
tags := d.Get("tags").(map[string]interface{}) | ||
|
||
parameters := eventhub.NamespaceCreateOrUpdateParameters{ | ||
Location: &location, | ||
Sku: &eventhub.Sku{ | ||
Name: eventhub.SkuName(sku), | ||
Tier: eventhub.SkuTier(sku), | ||
Capacity: &capacity, | ||
}, | ||
Tags: expandTags(tags), | ||
} | ||
|
||
_, err := namespaceClient.CreateOrUpdate(resGroup, name, parameters, make(chan struct{})) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
read, err := namespaceClient.Get(resGroup, name) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if read.ID == nil { | ||
return fmt.Errorf("Cannot read EventHub Namespace %s (resource group %s) ID", name, resGroup) | ||
} | ||
|
||
d.SetId(*read.ID) | ||
|
||
return resourceArmEventHubNamespaceRead(d, meta) | ||
} | ||
|
||
func resourceArmEventHubNamespaceRead(d *schema.ResourceData, meta interface{}) error { | ||
namespaceClient := meta.(*ArmClient).eventHubNamespacesClient | ||
|
||
id, err := parseAzureResourceID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
resGroup := id.ResourceGroup | ||
name := id.Path["namespaces"] | ||
|
||
resp, err := namespaceClient.Get(resGroup, name) | ||
if err != nil { | ||
return fmt.Errorf("Error making Read request on Azure EventHub Namespace %s: %s", name, err) | ||
} | ||
if resp.StatusCode == http.StatusNotFound { | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
d.Set("name", resp.Name) | ||
d.Set("location", azureRMNormalizeLocation(*resp.Location)) | ||
d.Set("resource_group_name", resGroup) | ||
d.Set("sku", string(resp.Sku.Name)) | ||
d.Set("capacity", resp.Sku.Capacity) | ||
|
||
keys, err := namespaceClient.ListKeys(resGroup, name, eventHubNamespaceDefaultAuthorizationRule) | ||
if err != nil { | ||
log.Printf("[ERROR] Unable to List default keys for Namespace %s: %s", name, err) | ||
} else { | ||
d.Set("default_primary_connection_string", keys.PrimaryConnectionString) | ||
d.Set("default_secondary_connection_string", keys.SecondaryConnectionString) | ||
d.Set("default_primary_key", keys.PrimaryKey) | ||
d.Set("default_secondary_key", keys.SecondaryKey) | ||
} | ||
|
||
flattenAndSetTags(d, resp.Tags) | ||
|
||
return nil | ||
} | ||
|
||
func resourceArmEventHubNamespaceDelete(d *schema.ResourceData, meta interface{}) error { | ||
|
||
namespaceClient := meta.(*ArmClient).eventHubNamespacesClient | ||
|
||
id, err := parseAzureResourceID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
resGroup := id.ResourceGroup | ||
name := id.Path["namespaces"] | ||
|
||
resp, err := namespaceClient.Delete(resGroup, name, make(chan struct{})) | ||
|
||
if resp.StatusCode != http.StatusNotFound { | ||
return fmt.Errorf("Error issuing Azure ARM delete request of EventHub Namespace'%s': %s", name, err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func validateEventHubNamespaceSku(v interface{}, k string) (ws []string, errors []error) { | ||
value := strings.ToLower(v.(string)) | ||
skus := map[string]bool{ | ||
"basic": true, | ||
"standard": true, | ||
} | ||
|
||
if !skus[value] { | ||
errors = append(errors, fmt.Errorf("EventHub Namespace SKU can only be Basic or Standard")) | ||
} | ||
return | ||
} | ||
|
||
func validateEventHubNamespaceCapacity(v interface{}, k string) (ws []string, errors []error) { | ||
value := v.(int) | ||
capacities := map[int]bool{ | ||
1: true, | ||
2: true, | ||
4: true, | ||
} | ||
|
||
if !capacities[value] { | ||
errors = append(errors, fmt.Errorf("EventHub Namespace Capacity can only be 1, 2 or 4")) | ||
} | ||
return | ||
} |
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.
Can you add a test to make sure that the import works?
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.
Added a test but it's failing - posting this here for later as a reminder of what needs fixing: