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

azurerm_eventhub_namespace - separate create and update funcs #16371

Merged
merged 2 commits into from
Apr 14, 2022
Merged
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
104 changes: 91 additions & 13 deletions internal/services/eventhub/eventhub_namespace_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ var (

func resourceEventHubNamespace() *pluginsdk.Resource {
return &pluginsdk.Resource{
Create: resourceEventHubNamespaceCreateUpdate,
Create: resourceEventHubNamespaceCreate,
Read: resourceEventHubNamespaceRead,
Update: resourceEventHubNamespaceCreateUpdate,
Update: resourceEventHubNamespaceUpdate,
Delete: resourceEventHubNamespaceDelete,

Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
Expand Down Expand Up @@ -250,27 +250,105 @@ func resourceEventHubNamespace() *pluginsdk.Resource {
}
}

func resourceEventHubNamespaceCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
func resourceEventHubNamespaceCreate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Eventhub.NamespacesClient
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()
log.Printf("[INFO] preparing arguments for AzureRM EventHub Namespace creation.")

id := namespaces.NewNamespaceID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))
if d.IsNewResource() {
existing, err := client.Get(ctx, id)
if err != nil {
if !response.WasNotFound(existing.HttpResponse) {
return fmt.Errorf("checking for presence of existing %s: %+v", id, err)
}
existing, err := client.Get(ctx, id)
if err != nil {
if !response.WasNotFound(existing.HttpResponse) {
return fmt.Errorf("checking for presence of existing %s: %+v", id, err)
}
}

if existing.Model != nil {
return tf.ImportAsExistsError("azurerm_eventhub_namespace", id.ID())
}

location := azure.NormalizeLocation(d.Get("location").(string))
sku := d.Get("sku").(string)
capacity := int32(d.Get("capacity").(int))
t := d.Get("tags").(map[string]interface{})
autoInflateEnabled := d.Get("auto_inflate_enabled").(bool)
zoneRedundant := d.Get("zone_redundant").(bool)

identity, err := expandEventHubIdentity(d.Get("identity").([]interface{}))
if err != nil {
return fmt.Errorf("expanding `identity`: %+v", err)
}

parameters := namespaces.EHNamespace{
Location: &location,
Sku: &namespaces.Sku{
Name: namespaces.SkuName(sku),
Tier: func() *namespaces.SkuTier {
v := namespaces.SkuTier(sku)
return &v
}(),
Capacity: utils.Int64(int64(capacity)),
},
Identity: identity,
Properties: &namespaces.EHNamespaceProperties{
IsAutoInflateEnabled: utils.Bool(autoInflateEnabled),
ZoneRedundant: utils.Bool(zoneRedundant),
},
Tags: tags.Expand(t),
}

if v := d.Get("dedicated_cluster_id").(string); v != "" {
parameters.Properties.ClusterArmId = utils.String(v)
}

if existing.Model != nil {
return tf.ImportAsExistsError("azurerm_eventhub_namespace", id.ID())
if v, ok := d.GetOk("maximum_throughput_units"); ok {
parameters.Properties.MaximumThroughputUnits = utils.Int64(int64(v.(int)))
}

if err := client.CreateOrUpdateThenPoll(ctx, id, parameters); err != nil {
return fmt.Errorf("creating %s: %+v", id, err)
}

d.SetId(id.ID())

ruleSets, hasRuleSets := d.GetOk("network_rulesets")
if hasRuleSets {
rulesets := networkrulesets.NetworkRuleSet{
Properties: expandEventHubNamespaceNetworkRuleset(ruleSets.([]interface{})),
}

// cannot use network rulesets with the basic SKU
if parameters.Sku.Name != namespaces.SkuNameBasic {
ruleSetsClient := meta.(*clients.Client).Eventhub.NetworkRuleSetsClient
namespaceId := networkrulesets.NewNamespaceID(id.SubscriptionId, id.ResourceGroupName, id.NamespaceName)
if _, err := ruleSetsClient.NamespacesCreateOrUpdateNetworkRuleSet(ctx, namespaceId, rulesets); err != nil {
return fmt.Errorf("setting network ruleset properties for %s: %+v", id, err)
}
} else if rulesets.Properties != nil {
props := rulesets.Properties
// so if the user has specified the non default rule sets throw a validation error
if *props.DefaultAction != networkrulesets.DefaultActionDeny ||
(props.IpRules != nil && len(*props.IpRules) > 0) ||
(props.VirtualNetworkRules != nil && len(*props.VirtualNetworkRules) > 0) {
return fmt.Errorf("network_rulesets cannot be used when the SKU is basic")
}
}
}

return resourceEventHubNamespaceRead(d, meta)
}

func resourceEventHubNamespaceUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Eventhub.NamespacesClient
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()
log.Printf("[INFO] preparing arguments for AzureRM EventHub Namespace update.")

id := namespaces.NewNamespaceID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))

location := azure.NormalizeLocation(d.Get("location").(string))
sku := d.Get("sku").(string)
capacity := int32(d.Get("capacity").(int))
Expand Down Expand Up @@ -318,8 +396,8 @@ func resourceEventHubNamespaceCreateUpdate(d *pluginsdk.ResourceData, meta inter
parameters.Properties.MaximumThroughputUnits = utils.Int64(0)
}

if err := client.CreateOrUpdateThenPoll(ctx, id, parameters); err != nil {
return fmt.Errorf("creating %s: %+v", id, err)
if _, err := client.Update(ctx, id, parameters); err != nil {
return fmt.Errorf("updating %s: %+v", id, err)
}

d.SetId(id.ID())
Expand Down