Skip to content

Commit

Permalink
fix: target_id for import
Browse files Browse the repository at this point in the history
  • Loading branch information
David MICHENEAU committed Oct 11, 2023
1 parent 94861fb commit d4eb29f
Show file tree
Hide file tree
Showing 16 changed files with 241 additions and 176 deletions.
8 changes: 4 additions & 4 deletions docs/data-sources/backup.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ data "cloudavenue_backup" "example" {

### Required

- `type` (String) Scope of the backup.
- `type` (String) Scope of the backup. Value must be one of : `vdc`, `vapp`, `vm`.

### Optional

- `id` (Number) The ID of the backup.
- `target_id` (String) The ID of the target. A target can be a VDC, a VApp or a VM.
- `target_name` (String) The name of the target. A target can be a VDC, a VApp or a VM.
- `target_id` (String) The URN of the target. A target can be a VDC, a VApp or a VM. Ensure that one and only one attribute from this collection is set : `target_id`, `target_name`. Must be a valid URN.
- `target_name` (String) The name of the target. A target can be a VDC, a VApp or a VM. Ensure that one and only one attribute from this collection is set : `target_id`, `target_name`.

### Read-Only

Expand All @@ -41,5 +41,5 @@ data "cloudavenue_backup" "example" {
Read-Only:

- `policy_id` (Number) The ID of the backup policy.
- `policy_name` (String) The name of the backup policy.
- `policy_name` (String) The name of the backup policy. Each letter represent a strategy predefined: D = Daily, W = Weekly, M = Monthly, X = Replication, The number is the retention period. [Please refer to the documentation for more information.](https://wiki.cloudavenue.orange-business.com/wiki/Backup).

15 changes: 8 additions & 7 deletions docs/resources/backup.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
page_title: "cloudavenue_backup Resource - cloudavenue"
subcategory: "Backup"
description: |-
The cloudavenue_backup resource allows you to manage backup strategy for vdc,vapp and 'vm' from NetBackup solution. Please refer to the documentation for more information. https://wiki.cloudavenue.orange-business.com/wiki/Backup
The cloudavenue_backup resource allows you to manage backup strategy for vdc, vapp and vm from NetBackup solution. Please refer to the documentation for more information. https://wiki.cloudavenue.orange-business.com/wiki/Backup
---

# cloudavenue_backup (Resource)

The `cloudavenue_backup` resource allows you to manage backup strategy for `vdc`,`vapp` and 'vm' from NetBackup solution. Please refer to the documentation for more information. https://wiki.cloudavenue.orange-business.com/wiki/Backup
The `cloudavenue_backup` resource allows you to manage backup strategy for `vdc`, `vapp` and `vm` from NetBackup solution. [Please refer to the documentation for more information.](https://wiki.cloudavenue.orange-business.com/wiki/Backup)

## Examples
### Example Usage of a VDC Backup with 2 policy sets
Expand All @@ -17,9 +17,10 @@ resource "cloudavenue_backup" "example-vdc" {
target_name = cloudavenue_vdc.example.name
policies = [{
policy_name = "D6"
} ,{
},
{
policy_name = "M3"
}
}
]
}
```
Expand All @@ -39,7 +40,7 @@ resource "cloudavenue_backup" "example-vapp" {
```hcl
resource "cloudavenue_backup" "example-vm" {
type = "vm"
target_name = "vmdemo"
target_name = cloudavenue_vm.example.name
policies = [{
policy_name = "D6"
}]
Expand All @@ -56,7 +57,7 @@ resource "cloudavenue_backup" "example-vm" {

### Optional

- `target_id` (String) (ForceNew) The ID of the target. A target can be a VDC, a VApp or a VM. Ensure that one and only one attribute from this collection is set : `target_id`, `target_name`. Must be a valid UUID.
- `target_id` (String) (ForceNew) The URN of the target. A target can be a VDC, a VApp or a VM. Ensure that one and only one attribute from this collection is set : `target_id`, `target_name`. Must be a valid URN.
- `target_name` (String) (ForceNew) The name of the target. A target can be a VDC, a VApp or a VM. Ensure that one and only one attribute from this collection is set : `target_id`, `target_name`.

### Read-Only
Expand All @@ -68,7 +69,7 @@ resource "cloudavenue_backup" "example-vm" {

Required:

- `policy_name` (String) The name of the backup policy. Each letter represent a strategy predefined: D = Daily, W = Weekly, M = Monthly, X = Replication, The number is the retention period. Please refer to the documentation for more information. https://wiki.cloudavenue.orange-business.com/wiki/Backup. Value must be one of : `D6`, `D30`, `D30NQ`, `D60`, `W4`, `M3`, `M12`, `XD6`, `XD30`, `XD60`, `XW4`, `XM3`, `XM12`.
- `policy_name` (String) The name of the backup policy. Each letter represent a strategy predefined: D = Daily, W = Weekly, M = Monthly, X = Replication, The number is the retention period. [Please refer to the documentation for more information.](https://wiki.cloudavenue.orange-business.com/wiki/Backup). Value must be one of : `D6`, `D30`, `D30NQ`, `D60`, `W4`, `M3`, `M12`, `XD6`, `XD30`, `XD60`, `XW4`, `XM3`, `XM12`.

Read-Only:

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ require (
github.com/hashicorp/terraform-plugin-log v0.9.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.29.0
github.com/iancoleman/strcase v0.3.0
github.com/orange-cloudavenue/cloudavenue-sdk-go v0.0.3-0.20231009201953-47ea587cab76
github.com/orange-cloudavenue/cloudavenue-sdk-go v0.0.4-0.20231010202218-e3886106bffa
github.com/orange-cloudavenue/infrapi-sdk-go v0.1.4-0.20231005074857-89878ea119fb
github.com/rs/zerolog v1.31.0
github.com/thanhpk/randstr v1.0.6
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE=
github.com/orange-cloudavenue/cloudavenue-sdk-go v0.0.3-0.20231009201953-47ea587cab76 h1:wMZjH50aktG4x7/B6LWF49vhdI/0Oj5/aWy6nu5EBHk=
github.com/orange-cloudavenue/cloudavenue-sdk-go v0.0.3-0.20231009201953-47ea587cab76/go.mod h1:DWBIS3DJtS5ZiZzblCwYNo123RaxO+UrGyfqsVQbFb0=
github.com/orange-cloudavenue/cloudavenue-sdk-go v0.0.4-0.20231010202218-e3886106bffa h1:98Mkhxg3OOqP66+S55oMVZnSichMsmiRH2kXc9NJAWU=
github.com/orange-cloudavenue/cloudavenue-sdk-go v0.0.4-0.20231010202218-e3886106bffa/go.mod h1:DWBIS3DJtS5ZiZzblCwYNo123RaxO+UrGyfqsVQbFb0=
github.com/orange-cloudavenue/infrapi-sdk-go v0.1.4-0.20231005074857-89878ea119fb h1:1/Wc21Tp9RnDOUTjKBm9x3wi+UgUkDc2bv0fHJc5f2o=
github.com/orange-cloudavenue/infrapi-sdk-go v0.1.4-0.20231005074857-89878ea119fb/go.mod h1:pGa9mB6s+weCi5QtNe5nicp7yL0C/e+i+3wHRh4cjBE=
github.com/peterhellberg/link v1.2.0 h1:UA5pg3Gp/E0F2WdX7GERiNrPQrM1K6CVJUUWfHa4t6c=
Expand Down
13 changes: 5 additions & 8 deletions internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,12 @@ func (c *CloudAvenue) New() (*CloudAvenue, error) {
}

// API NetBackup
// if c.NetBackup.Netbackup.Endpoint != "" || c.NetBackup.Netbackup.Username != "" || c.NetBackup.Netbackup.Password != "" {
c.NetBackupClient, err = clientca.New(*c.NetBackupOpts)
if err != nil {
return nil, fmt.Errorf("%w : %w", ErrConfigureNetBackup, err)
if c.NetBackupOpts.Netbackup.Username != "" && c.NetBackupOpts.Netbackup.Password != "" {
c.NetBackupClient, err = clientca.New(*c.NetBackupOpts)
if err != nil {
return nil, fmt.Errorf("%w : %w", ErrConfigureNetBackup, err)
}
}
// if err := c.NewNetBackupClient(); err != nil {
// return nil, err
// }
// }

return c, nil
}
Expand Down
11 changes: 11 additions & 0 deletions internal/provider/backup/backup_datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@ func (d *backupDataSource) Read(ctx context.Context, req datasource.ReadRequest,
client: d.client,
}

// Refresh data NetBackup from the API
job, err := d.client.NetBackupClient.V1.Netbackup.Inventory.Refresh()
if err != nil {
resp.Diagnostics.AddError("Error refreshing NetBackup inventory", err.Error())
return
}
if err := job.Wait(1, 45); err != nil {
resp.Diagnostics.AddError("Error waiting for NetBackup inventory refresh", err.Error())
return
}

// Read data from the API
data, _, diags := s.read(ctx, config)
resp.Diagnostics.Append(diags...)
Expand Down
15 changes: 12 additions & 3 deletions internal/provider/backup/backup_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ func (r *backupResource) Create(ctx context.Context, req resource.CreateRequest,
return
}

// // Set target name
// if plan.TargetName.IsNull() {
// plan.TargetName.Set(typeTarget.GetName())
// }

// Apply the protection levels policies for each policy
if err := applyPolicies(typeTarget, policies); err != nil {
resp.Diagnostics.AddError("Error applying protection levels", err.Error())
Expand Down Expand Up @@ -324,6 +329,7 @@ type target interface {
Protect(v1.ProtectUnprotectRequest) (*v1common.JobAPIResponse, error)
Unprotect(v1.ProtectUnprotectRequest) (*v1common.JobAPIResponse, error)
GetID() int
GetName() string
ListProtectionLevels() (*v1.ProtectionLevels, error)
}

Expand All @@ -340,7 +346,7 @@ func applyPolicy[T target](t T, policy backupModelPolicy) (backupModelPolicy, er
if err != nil {
return backupModelPolicy{}, err
}
if err := job.Wait(1, 15); err != nil {
if err := job.Wait(1, 30); err != nil {
return backupModelPolicy{}, err
}

Expand Down Expand Up @@ -404,8 +410,6 @@ func unApplyPolicy[T target](t T, policy backupModelPolicy) error {
func (r *backupResource) read(ctx context.Context, planOrState *backupModel) (stateRefreshed *backupModel, found bool, diags diag.Diagnostics) {
stateRefreshed = planOrState.Copy()

var policiesFromAPI *v1.ProtectionLevels // *netbackupclient.ProtectionLevels

// Get the type target object
typeTarget, d := r.getTarget(planOrState)
if d.HasError() {
Expand All @@ -425,6 +429,11 @@ func (r *backupResource) read(ctx context.Context, planOrState *backupModel) (st
return nil, true, diags
}

// Set target name
if !planOrState.TargetName.IsKnown() {
stateRefreshed.TargetName.Set(typeTarget.GetName())
}

// Add policies from API
policies := backupModelPolicies{}
for _, policyFromAPI := range *policiesFromAPI {
Expand Down
20 changes: 9 additions & 11 deletions internal/provider/backup/backup_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,26 +48,26 @@ func backupSchema(_ context.Context) superschema.Schema {
Common: &schemaR.StringAttribute{
MarkdownDescription: "Scope of the backup.",
Required: true,
},
Resource: &schemaR.StringAttribute{
Validators: []validator.String{
stringvalidator.OneOf("vdc", "vapp", "vm"),
},
},
Resource: &schemaR.StringAttribute{
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
},
"target_id": superschema.SuperStringAttribute{
Common: &schemaR.StringAttribute{
MarkdownDescription: "The ID of the target. A target can be a VDC, a VApp or a VM.",
MarkdownDescription: "The URN of the target. A target can be a VDC, a VApp or a VM.",
Optional: true,
},
Resource: &schemaR.StringAttribute{
Validators: []validator.String{
stringvalidator.ExactlyOneOf(path.MatchRoot("target_id"), path.MatchRoot("target_name")),
fstringvalidator.IsUUID(),
fstringvalidator.IsURN(),
},
},
Resource: &schemaR.StringAttribute{
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplaceIfConfigured(),
stringplanmodifier.UseStateForUnknown(),
Expand All @@ -81,19 +81,17 @@ func backupSchema(_ context.Context) superschema.Schema {
Common: &schemaR.StringAttribute{
MarkdownDescription: "The name of the target. A target can be a VDC, a VApp or a VM.",
Optional: true,
},
Resource: &schemaR.StringAttribute{
Computed: true,
Validators: []validator.String{
stringvalidator.ExactlyOneOf(path.MatchRoot("target_id"), path.MatchRoot("target_name")),
},
},
Resource: &schemaR.StringAttribute{
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplaceIfConfigured(),
stringplanmodifier.UseStateForUnknown(),
},
},
DataSource: &schemaD.StringAttribute{
Computed: true,
},
},
"policies": superschema.SuperSetNestedAttribute{
Common: &schemaR.SetNestedAttribute{
Expand Down
2 changes: 0 additions & 2 deletions internal/provider/backup/backup_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
netbackup "github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/provider/backup"
)

// TODO : Comment or uncomment the following imports if you are using resources or/and datasources

// Unit test for the schema of the resource cloudavenue_netbackup_Backup.
func TestBackupResourceSchema(t *testing.T) {
t.Parallel()
Expand Down
44 changes: 19 additions & 25 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
casdk "github.com/orange-cloudavenue/cloudavenue-sdk-go"
clientcloudavenue "github.com/orange-cloudavenue/cloudavenue-sdk-go/pkg/clients/cloudavenue"
clientnetbackup "github.com/orange-cloudavenue/cloudavenue-sdk-go/pkg/clients/netbackup"

"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/client"
)

Expand Down Expand Up @@ -112,30 +111,25 @@ func (p *cloudavenueProvider) Configure(ctx context.Context, req provider.Config
)
}

// if cloudAvenue.NetBackupOpts.IsDefined() {
// // Default URL to the public NetBackup API if not set.
// if cloudAvenue.NetBackupOpts.URL == "" {
// cloudAvenue.NetBackupOpts.URL = "https://backup1.cloudavenue.orange-business.com/NetBackupSelfServiceNetBackupPanels/Api"
// }
// if cloudAvenue.NetBackup.User == "" {
// resp.Diagnostics.AddAttributeError(
// path.Root("netbackup_user"),
// "Missing NetBackup API User",
// "The provider cannot create the NetBackup API client as there is a missing or empty value for the NetBackup API user. "+
// "Set the host value in the configuration or use the NETBACKUP_USER environment variable. "+
// "If either is already set, ensure the value is not empty.",
// )
// }
// if cloudAvenue.NetBackup.Password == "" {
// resp.Diagnostics.AddAttributeError(
// path.Root("netbackup_password"),
// "Missing NetBackup API Password",
// "The provider cannot create the NetBackup API client as there is a missing or empty value for the NetBackup API password. "+
// "Set the host value in the configuration or use the NETBACKUP_PASSWORD environment variable. "+
// "If either is already set, ensure the value is not empty.",
// )
// }
// }
if cloudAvenue.NetBackupOpts.Netbackup.Username == "" {
resp.Diagnostics.AddAttributeError(
path.Root("netbackup_user"),
"Missing NetBackup API User",
"The provider cannot create the NetBackup API client as there is a missing or empty value for the NetBackup API user. "+
"Set the host value in the configuration or use the NETBACKUP_USER environment variable. "+
"If either is already set, ensure the value is not empty.",
)
}
if cloudAvenue.NetBackupOpts.Netbackup.Password == "" {
resp.Diagnostics.AddAttributeError(
path.Root("netbackup_password"),
"Missing NetBackup API Password",
"The provider cannot create the NetBackup API client as there is a missing or empty value for the NetBackup API password. "+
"Set the host value in the configuration or use the NETBACKUP_PASSWORD environment variable. "+
"If either is already set, ensure the value is not empty.",
)
}

if resp.Diagnostics.HasError() {
return
}
Expand Down
3 changes: 3 additions & 0 deletions internal/testsacc/acctest_datasources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@ func GetDataSourceConfig() map[testsacc.ResourceName]func() resourceConfig {
// * VDC
VDCDataSourceName: NewResourceConfig(NewVDCDataSourceTest()),
VDCGroupDataSourceName: NewResourceConfig(NewVDCGroupDataSourceTest()),

// * Backup
BackupDataSourceName: NewResourceConfig(NewBackupDataSourceTest()),
}
}
3 changes: 3 additions & 0 deletions internal/testsacc/acctest_resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,8 @@ func GetResourceConfig() map[testsacc.ResourceName]func() resourceConfig {

// * Edge Gateway
EdgeGatewayResourceName: NewResourceConfig(NewEdgeGatewayResourceTest()),

// * Backup
BackupResourceName: NewResourceConfig(NewBackupResourceTest()),
}
}
Loading

0 comments on commit d4eb29f

Please sign in to comment.