@@ -33,7 +33,6 @@ import (
3333 "github.com/hashicorp/terraform-provider-azurerm/internal/tf/suppress"
3434 "github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
3535 "github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
36- "github.com/hashicorp/terraform-provider-azurerm/utils"
3736)
3837
3938// Default Authorization Rule/Policy created by Azure, used to populate the
4544
4645func resourceServiceBusNamespace () * pluginsdk.Resource {
4746 resource := & pluginsdk.Resource {
48- Create : resourceServiceBusNamespaceCreateUpdate ,
47+ Create : resourceServiceBusNamespaceCreate ,
4948 Read : resourceServiceBusNamespaceRead ,
50- Update : resourceServiceBusNamespaceCreateUpdate ,
49+ Update : resourceServiceBusNamespaceUpdate ,
5150 Delete : resourceServiceBusNamespaceDelete ,
5251
5352 Importer : pluginsdk .ImporterValidatingResourceId (func (id string ) error {
@@ -284,32 +283,31 @@ func resourceServiceBusNamespace() *pluginsdk.Resource {
284283 return resource
285284}
286285
287- func resourceServiceBusNamespaceCreateUpdate (d * pluginsdk.ResourceData , meta interface {}) error {
286+ func resourceServiceBusNamespaceCreate (d * pluginsdk.ResourceData , meta interface {}) error {
288287 client := meta .(* clients.Client ).ServiceBus .NamespacesClient
289288 subscriptionId := meta .(* clients.Client ).Account .SubscriptionId
290- ctx , cancel := timeouts .ForCreateUpdate (meta .(* clients.Client ).StopContext , d )
289+ ctx , cancel := timeouts .ForCreate (meta .(* clients.Client ).StopContext , d )
291290 defer cancel ()
292291
293- log .Printf ("[INFO] preparing arguments for ServiceBus Namespace create/update. " )
292+ log .Printf ("[INFO] preparing arguments for ServiceBus Namespace create" )
294293
295294 location := azure .NormalizeLocation (d .Get ("location" ).(string ))
296295 sku := d .Get ("sku" ).(string )
297296 t := d .Get ("tags" ).(map [string ]interface {})
298297
299298 id := namespaces .NewNamespaceID (subscriptionId , d .Get ("resource_group_name" ).(string ), d .Get ("name" ).(string ))
300- if d .IsNewResource () {
301- existing , err := client .Get (ctx , id )
302- if err != nil {
303- if ! response .WasNotFound (existing .HttpResponse ) {
304- return fmt .Errorf ("checking for presence of existing %s: %+v" , id , err )
305- }
306- }
307299
300+ existing , err := client .Get (ctx , id )
301+ if err != nil {
308302 if ! response .WasNotFound (existing .HttpResponse ) {
309- return tf . ImportAsExistsError ( "azurerm_servicebus_namespace " , id . ID () )
303+ return fmt . Errorf ( "checking for presence of existing %s: %+v " , id , err )
310304 }
311305 }
312306
307+ if ! response .WasNotFound (existing .HttpResponse ) {
308+ return tf .ImportAsExistsError ("azurerm_servicebus_namespace" , id .ID ())
309+ }
310+
313311 identity , err := expandSystemAndUserAssignedMap (d .Get ("identity" ).([]interface {}))
314312 if err != nil {
315313 return fmt .Errorf ("expanding `identity`: %+v" , err )
@@ -330,7 +328,7 @@ func resourceServiceBusNamespaceCreateUpdate(d *pluginsdk.ResourceData, meta int
330328 },
331329 Properties : & namespaces.SBNamespaceProperties {
332330 Encryption : expandServiceBusNamespaceEncryption (d .Get ("customer_managed_key" ).([]interface {})),
333- DisableLocalAuth : utils . Bool (! d .Get ("local_auth_enabled" ).(bool )),
331+ DisableLocalAuth : pointer . To (! d .Get ("local_auth_enabled" ).(bool )),
334332 PublicNetworkAccess : & publicNetworkEnabled ,
335333 },
336334 Tags : expandTags (t ),
@@ -348,7 +346,7 @@ func resourceServiceBusNamespaceCreateUpdate(d *pluginsdk.ResourceData, meta int
348346 if strings .EqualFold (sku , string (namespaces .SkuNamePremium )) && capacity .(int ) == 0 {
349347 return fmt .Errorf ("service bus SKU %q only supports `capacity` of 1, 2, 4, 8 or 16" , sku )
350348 }
351- parameters .Sku .Capacity = utils . Int64 (int64 (capacity .(int )))
349+ parameters .Sku .Capacity = pointer . To (int64 (capacity .(int )))
352350 }
353351
354352 if premiumMessagingUnit := d .Get ("premium_messaging_partitions" ); premiumMessagingUnit != nil {
@@ -358,11 +356,104 @@ func resourceServiceBusNamespaceCreateUpdate(d *pluginsdk.ResourceData, meta int
358356 if strings .EqualFold (sku , string (namespaces .SkuNamePremium )) && premiumMessagingUnit .(int ) == 0 {
359357 return fmt .Errorf ("service bus SKU %q only supports `premium_messaging_partitions` of 1, 2, 4" , sku )
360358 }
361- parameters .Properties .PremiumMessagingPartitions = utils . Int64 (int64 (premiumMessagingUnit .(int )))
359+ parameters .Properties .PremiumMessagingPartitions = pointer . To (int64 (premiumMessagingUnit .(int )))
362360 }
363361
364362 if err := client .CreateOrUpdateThenPoll (ctx , id , parameters ); err != nil {
365- return fmt .Errorf ("creating/updating %s: %+v" , id , err )
363+ return fmt .Errorf ("creating %s: %+v" , id , err )
364+ }
365+
366+ d .SetId (id .ID ())
367+
368+ if err = createNetworkRuleSetForNamespace (ctx , client , id , d .Get ("network_rule_set" ).([]interface {})); err != nil {
369+ return err
370+ }
371+
372+ return resourceServiceBusNamespaceRead (d , meta )
373+ }
374+
375+ func resourceServiceBusNamespaceUpdate (d * pluginsdk.ResourceData , meta interface {}) error {
376+ client := meta .(* clients.Client ).ServiceBus .NamespacesClient
377+ ctx , cancel := timeouts .ForUpdate (meta .(* clients.Client ).StopContext , d )
378+ defer cancel ()
379+
380+ log .Printf ("[INFO] preparing arguments for ServiceBus Namespace update" )
381+
382+ id , err := namespaces .ParseNamespaceID (d .Id ())
383+ if err != nil {
384+ return err
385+ }
386+
387+ existing , err := client .Get (ctx , * id )
388+ if err != nil {
389+ return fmt .Errorf ("retrieving %s: %+v" , * id , err )
390+ }
391+
392+ if existing .Model == nil {
393+ return fmt .Errorf ("retrieving %s: `model` was nil" , * id )
394+ }
395+ if existing .Model .Properties == nil {
396+ return fmt .Errorf ("retrieving %s: `model.Properties` was nil" , * id )
397+ }
398+
399+ payload := existing .Model
400+
401+ if d .HasChange ("identity" ) {
402+ identity , err := expandSystemAndUserAssignedMap (d .Get ("identity" ).([]interface {}))
403+ if err != nil {
404+ return fmt .Errorf ("expanding `identity`: %+v" , err )
405+ }
406+ payload .Identity = identity
407+ }
408+
409+ if d .HasChange ("public_network_access_enabled" ) {
410+ publicNetworkEnabled := namespaces .PublicNetworkAccessEnabled
411+ if ! d .Get ("public_network_access_enabled" ).(bool ) {
412+ publicNetworkEnabled = namespaces .PublicNetworkAccessDisabled
413+ }
414+ payload .Properties .PublicNetworkAccess = & publicNetworkEnabled
415+ }
416+
417+ if d .HasChange ("sku" ) {
418+ sku := d .Get ("sku" ).(string )
419+ s := namespaces .SkuTier (sku )
420+ payload .Sku = & namespaces.SBSku {
421+ Name : namespaces .SkuName (sku ),
422+ Tier : & s ,
423+ }
424+ }
425+
426+ if d .HasChange ("customer_managed_key" ) {
427+ payload .Properties .Encryption = expandServiceBusNamespaceEncryption (d .Get ("customer_managed_key" ).([]interface {}))
428+ }
429+
430+ if d .HasChange ("local_auth_enabled" ) {
431+ payload .Properties .DisableLocalAuth = pointer .To (! d .Get ("local_auth_enabled" ).(bool ))
432+ }
433+
434+ if d .HasChange ("tags" ) {
435+ payload .Tags = expandTags (d .Get ("tags" ).(map [string ]interface {}))
436+ }
437+
438+ if d .HasChange ("minimum_tls_version" ) {
439+ payload .Properties .MinimumTlsVersion = pointer .To (namespaces .TlsVersion (d .Get ("minimum_tls_version" ).(string )))
440+ }
441+
442+ if d .HasChange ("capacity" ) {
443+ sku := d .Get ("sku" ).(string )
444+ if capacity := d .Get ("capacity" ); capacity != nil {
445+ if ! strings .EqualFold (sku , string (namespaces .SkuNamePremium )) && capacity .(int ) > 0 {
446+ return fmt .Errorf ("service bus SKU %q only supports `capacity` of 0" , sku )
447+ }
448+ if strings .EqualFold (sku , string (namespaces .SkuNamePremium )) && capacity .(int ) == 0 {
449+ return fmt .Errorf ("service bus SKU %q only supports `capacity` of 1, 2, 4, 8 or 16" , sku )
450+ }
451+ payload .Sku .Capacity = pointer .To (int64 (capacity .(int )))
452+ }
453+ }
454+
455+ if err := client .CreateOrUpdateThenPoll (ctx , * id , * payload ); err != nil {
456+ return fmt .Errorf ("updating %s: %+v" , id , err )
366457 }
367458
368459 d .SetId (id .ID ())
@@ -372,16 +463,16 @@ func resourceServiceBusNamespaceCreateUpdate(d *pluginsdk.ResourceData, meta int
372463 // if the network rule set has been removed from config, reset it instead as there is no way to remove a rule set
373464 if len (oldNetworkRuleSet .([]interface {})) == 1 && len (newNetworkRuleSet .([]interface {})) == 0 {
374465 log .Printf ("[DEBUG] Resetting Network Rule Set associated with %s.." , id )
375- if err = resetNetworkRuleSetForNamespace (ctx , client , id ); err != nil {
466+ if err = resetNetworkRuleSetForNamespace (ctx , client , * id ); err != nil {
376467 return err
377468 }
378469 log .Printf ("[DEBUG] Reset the Existing Network Rule Set associated with %s" , id )
379470 } else {
380- log .Printf ("[DEBUG] Creating the Network Rule Set associated with %s.." , id )
381- if err = createNetworkRuleSetForNamespace (ctx , client , id , newNetworkRuleSet .([]interface {})); err != nil {
471+ log .Printf ("[DEBUG] Updating the Network Rule Set associated with %s.." , id )
472+ if err = createNetworkRuleSetForNamespace (ctx , client , * id , newNetworkRuleSet .([]interface {})); err != nil {
382473 return err
383474 }
384- log .Printf ("[DEBUG] Created the Network Rule Set associated with %s" , id )
475+ log .Printf ("[DEBUG] Updated the Network Rule Set associated with %s" , id )
385476 }
386477 }
387478
@@ -519,20 +610,24 @@ func expandServiceBusNamespaceEncryption(input []interface{}) *namespaces.Encryp
519610 v := input [0 ].(map [string ]interface {})
520611 keyId , _ := keyVaultParse .ParseOptionallyVersionedNestedItemID (v ["key_vault_key_id" ].(string ))
521612 keySource := namespaces .KeySourceMicrosoftPointKeyVault
522- return & namespaces.Encryption {
523- KeyVaultProperties : & []namespaces.KeyVaultProperties {
524- {
525- KeyName : utils .String (keyId .Name ),
526- KeyVersion : utils .String (keyId .Version ),
527- KeyVaultUri : utils .String (keyId .KeyVaultBaseUrl ),
528- Identity : & namespaces.UserAssignedIdentityProperties {
529- UserAssignedIdentity : utils .String (v ["identity_id" ].(string )),
530- },
613+
614+ encryption := namespaces.Encryption {
615+ KeySource : & keySource ,
616+ RequireInfrastructureEncryption : pointer .To (v ["infrastructure_encryption_enabled" ].(bool )),
617+ }
618+
619+ encryption .KeyVaultProperties = & []namespaces.KeyVaultProperties {
620+ {
621+ KeyName : pointer .To (keyId .Name ),
622+ KeyVersion : pointer .To (keyId .Version ),
623+ KeyVaultUri : pointer .To (keyId .KeyVaultBaseUrl ),
624+ Identity : & namespaces.UserAssignedIdentityProperties {
625+ UserAssignedIdentity : pointer .To (v ["identity_id" ].(string )),
531626 },
532627 },
533- KeySource : & keySource ,
534- RequireInfrastructureEncryption : utils .Bool (v ["infrastructure_encryption_enabled" ].(bool )),
535628 }
629+
630+ return & encryption
536631}
537632
538633func flattenServiceBusNamespaceEncryption (encryption * namespaces.Encryption ) ([]interface {}, error ) {
@@ -620,6 +715,9 @@ func createNetworkRuleSetForNamespace(ctx context.Context, client *namespaces.Na
620715 if len (input ) < 1 || input [0 ] == nil {
621716 return nil
622717 }
718+
719+ log .Printf ("[DEBUG] Creating/updating the Network Rule Set associated with %s.." , id )
720+
623721 item := input [0 ].(map [string ]interface {})
624722
625723 defaultAction := namespaces .DefaultAction (item ["default_action" ].(string ))
@@ -643,13 +741,14 @@ func createNetworkRuleSetForNamespace(ctx context.Context, client *namespaces.Na
643741 VirtualNetworkRules : vnetRule ,
644742 IPRules : ipRule ,
645743 PublicNetworkAccess : & publicNetworkAccess ,
646- TrustedServiceAccessEnabled : utils . Bool (item ["trusted_services_allowed" ].(bool )),
744+ TrustedServiceAccessEnabled : pointer . To (item ["trusted_services_allowed" ].(bool )),
647745 },
648746 }
649747
650748 if _ , err := client .CreateOrUpdateNetworkRuleSet (ctx , id , parameters ); err != nil {
651749 return fmt .Errorf ("creating/updating %s: %+v" , id , err )
652750 }
751+ log .Printf ("[DEBUG] Created/updated the Network Rule Set associated with %s" , id )
653752
654753 return nil
655754}
@@ -687,17 +786,6 @@ func flattenServiceBusNamespaceNetworkRuleSet(networkRuleSet namespaces.NetworkR
687786 networkRules := flattenServiceBusNamespaceVirtualNetworkRules (networkRuleSet .VirtualNetworkRules )
688787 ipRules := flattenServiceBusNamespaceIPRules (networkRuleSet .IPRules )
689788
690- // only set network rule set if the values are different than what they are defaulted to during namespace creation
691- // this has to wait until 4.0 due to `azurerm_servicebus_namespace_network_rule_set` which forces `network_rule_set` to be Optional/Computed
692-
693- if defaultAction == string (namespaces .DefaultActionAllow ) &&
694- publicNetworkAccess == namespaces .PublicNetworkAccessFlagEnabled &&
695- ! trustedServiceEnabled &&
696- len (networkRules ) == 0 &&
697- len (ipRules ) == 0 {
698- return []interface {}{}
699- }
700-
701789 return []interface {}{map [string ]interface {}{
702790 "default_action" : defaultAction ,
703791 "trusted_services_allowed" : trustedServiceEnabled ,
0 commit comments