-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New Resource/Datasource
azurerm_storage_sync_group
(#8462)
- Loading branch information
Showing
12 changed files
with
639 additions
and
0 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
68 changes: 68 additions & 0 deletions
68
azurerm/internal/services/storage/data_source_storage_sync_group.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,68 @@ | ||
package storage | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/storage/parsers" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/storage/validate" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" | ||
) | ||
|
||
func dataSourceArmStorageSyncGroup() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceArmStorageSyncGroupRead, | ||
|
||
Timeouts: &schema.ResourceTimeout{ | ||
Read: schema.DefaultTimeout(5 * time.Minute), | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validate.StorageSyncName, | ||
}, | ||
|
||
"storage_sync_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validate.StorageSyncId, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceArmStorageSyncGroupRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*clients.Client).Storage.StoragesyncGroupClient | ||
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) | ||
defer cancel() | ||
|
||
name := d.Get("name").(string) | ||
ssID := d.Get("storage_sync_id").(string) | ||
ssId, _ := parsers.ParseStorageSyncID(ssID) | ||
|
||
resp, err := client.Get(ctx, ssId.ResourceGroup, ssId.Name, name) | ||
if err != nil { | ||
if utils.ResponseWasNotFound(resp.Response) { | ||
log.Printf("[INFO] Storage Sync Group %q does not exist - removing from state", name) | ||
d.SetId("") | ||
return nil | ||
} | ||
return fmt.Errorf("reading Storage Sync Group %q (Storage Sync Name %q / Resource Group %q): %+v", name, ssId.Name, ssId.ResourceGroup, err) | ||
} | ||
|
||
if resp.ID == nil || *resp.ID == "" { | ||
return fmt.Errorf("reading Storage Sync Group %q (Storage Sync Name %q /Resource Group %q) ID is empty or nil", name, ssId.Name, ssId.ResourceGroup) | ||
} | ||
|
||
d.SetId(*resp.ID) | ||
|
||
d.Set("name", name) | ||
d.Set("storage_sync_id", ssID) | ||
return nil | ||
} |
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
142 changes: 142 additions & 0 deletions
142
azurerm/internal/services/storage/resource_arm_storage_sync_group.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,142 @@ | ||
package storage | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
"github.com/Azure/azure-sdk-for-go/services/storagesync/mgmt/2020-03-01/storagesync" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/storage/parsers" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/storage/validate" | ||
azSchema "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/schema" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" | ||
) | ||
|
||
func resourceArmStorageSyncGroup() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceArmStorageSyncGroupCreate, | ||
Read: resourceArmStorageSyncGroupRead, | ||
Delete: resourceArmStorageSyncGroupDelete, | ||
|
||
Importer: azSchema.ValidateResourceIDPriorToImport(func(id string) error { | ||
_, err := parsers.StorageSyncGroupID(id) | ||
return err | ||
}), | ||
|
||
Timeouts: &schema.ResourceTimeout{ | ||
Create: schema.DefaultTimeout(30 * time.Minute), | ||
Read: schema.DefaultTimeout(5 * time.Minute), | ||
Delete: schema.DefaultTimeout(30 * time.Minute), | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validate.StorageSyncName, | ||
}, | ||
|
||
"storage_sync_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validate.StorageSyncId, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceArmStorageSyncGroupCreate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*clients.Client).Storage.StoragesyncGroupClient | ||
ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d) | ||
defer cancel() | ||
|
||
name := d.Get("name").(string) | ||
ssId, _ := parsers.ParseStorageSyncID(d.Get("storage_sync_id").(string)) | ||
|
||
existing, err := client.Get(ctx, ssId.ResourceGroup, ssId.Name, name) | ||
if err != nil { | ||
if !utils.ResponseWasNotFound(existing.Response) { | ||
return fmt.Errorf("checking for presence of existing Storage Sync Group (Storage Sync Group Name %q / Storage Sync Name %q /Resource Group %q): %+v", name, ssId.Name, ssId.ResourceGroup, err) | ||
} | ||
} | ||
if existing.ID != nil && *existing.ID != "" { | ||
return tf.ImportAsExistsError("azurerm_storage_sync_group", *existing.ID) | ||
} | ||
|
||
if _, err := client.Create(ctx, ssId.ResourceGroup, ssId.Name, name, storagesync.SyncGroupCreateParameters{}); err != nil { | ||
return fmt.Errorf("creating Storage Sync Group (Storage Sync Group Name %q / Storage Sync Name %q /Resource Group %q): %+v", name, ssId.Name, ssId.ResourceGroup, err) | ||
} | ||
|
||
resp, err := client.Get(ctx, ssId.ResourceGroup, ssId.Name, name) | ||
if err != nil { | ||
return fmt.Errorf("retrieving Storage Sync Group (Storage Sync Group Name %q / Storage Sync Name %q /Resource Group %q): %+v", name, ssId.Name, ssId.ResourceGroup, err) | ||
} | ||
|
||
if resp.ID == nil || *resp.ID == "" { | ||
return fmt.Errorf("reading Storage Sync Group (Storage Sync Group Name %q / Storage Sync Name %q /Resource Group %q) ID is empty or nil", name, ssId.Name, ssId.ResourceGroup) | ||
} | ||
|
||
d.SetId(*resp.ID) | ||
|
||
return resourceArmStorageSyncGroupRead(d, meta) | ||
} | ||
|
||
func resourceArmStorageSyncGroupRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*clients.Client).Storage.StoragesyncGroupClient | ||
ssClient := meta.(*clients.Client).Storage.SyncServiceClient | ||
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) | ||
defer cancel() | ||
|
||
id, err := parsers.StorageSyncGroupID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
resp, err := client.Get(ctx, id.ResourceGroup, id.StorageSyncName, id.Name) | ||
if err != nil { | ||
if utils.ResponseWasNotFound(resp.Response) { | ||
log.Printf("[INFO] Storage Sync Group %q does not exist - removing from state", d.Id()) | ||
d.SetId("") | ||
return nil | ||
} | ||
return fmt.Errorf("reading Storage Sync Group (Storage Sync Group Name %q / Storage Sync Name %q /Resource Group %q): %+v", id.Name, id.StorageSyncName, id.ResourceGroup, err) | ||
} | ||
|
||
d.Set("name", resp.Name) | ||
|
||
ssResp, err := ssClient.Get(ctx, id.ResourceGroup, id.StorageSyncName) | ||
if err != nil { | ||
return fmt.Errorf("reading Storage Sync %q (Resource Group %q): %+v", id.StorageSyncName, id.ResourceGroup, err) | ||
} | ||
|
||
if ssResp.ID == nil || *ssResp.ID == "" { | ||
return fmt.Errorf("reading Storage Sync %q (Resource Group %q) ID is empty or nil", id.StorageSyncName, id.ResourceGroup) | ||
} | ||
|
||
d.Set("storage_sync_id", ssResp.ID) | ||
|
||
return nil | ||
} | ||
|
||
func resourceArmStorageSyncGroupDelete(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*clients.Client).Storage.StoragesyncGroupClient | ||
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d) | ||
defer cancel() | ||
|
||
id, err := parsers.StorageSyncGroupID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if _, err := client.Delete(ctx, id.ResourceGroup, id.StorageSyncName, id.Name); err != nil { | ||
return fmt.Errorf("deleting Storage Sync Group (Storage Sync Group Name %q / Storage Sync Name %q /Resource Group %q): %+v", id.Name, id.StorageSyncName, id.ResourceGroup, err) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.