-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathdatasource_backup_schedule.go
More file actions
103 lines (82 loc) · 3.67 KB
/
datasource_backup_schedule.go
File metadata and controls
103 lines (82 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// © Broadcom. All Rights Reserved.
// The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
// SPDX-License-Identifier: MPL-2.0
package backupschedule
import (
"context"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/pkg/errors"
"github.com/vmware/terraform-provider-tanzu-mission-control/internal/authctx"
backupschedulemodels "github.com/vmware/terraform-provider-tanzu-mission-control/internal/models/backupschedule/cluster"
"github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/backupschedule/scope"
commonscope "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/common/scope"
)
func DataSourceBackupSchedule() *schema.Resource {
// Unpack resource map to datasource map.
constructTFModelDataSourceResponseMap()
return &schema.Resource{
ReadContext: dataSourceBackupScheduleRead,
Schema: backupScheduleDataSourceSchema,
}
}
func dataSourceBackupScheduleRead(ctx context.Context, data *schema.ResourceData, m interface{}) (diags diag.Diagnostics) {
config := m.(authctx.TanzuContext)
backupScheduleName, ok := data.Get(NameKey).(string)
if !ok {
return diag.Errorf("Unable to read backup schedule name")
}
scopedFullnameData := scope.ConstructScope(data, backupScheduleName)
if scopedFullnameData == nil {
return diag.Errorf("Unable to get Tanzu Mission Control git repository entry; Scope full name is empty")
}
switch scopedFullnameData.Scope {
case commonscope.ClusterScope:
if scopedFullnameData.FullnameCluster != nil {
request, err := tfModelDataSourceRequestConverter.ConvertTFSchemaToAPIModel(data, []string{})
if err != nil {
return diag.FromErr(errors.Wrapf(err, "Couldn't read Tanzu Mission Control backup schedule."))
}
var resp *backupschedulemodels.VmwareTanzuManageV1alpha1ClusterDataprotectionScheduleListSchedulesResponse
resp, err = config.TMCConnection.BackupScheduleService.BackupScheduleResourceServiceList(request)
switch {
case err != nil:
return diag.FromErr(errors.Wrap(err, "Couldn't list backup schedules"))
case resp.Schedules == nil:
data.SetId("NO_DATA")
default:
err = tfModelDataSourceResponseConverter.FillTFSchema(resp, data)
if err != nil {
diags = diag.FromErr(err)
}
fullNameList := []string{request.SearchScope.ManagementClusterName, request.SearchScope.ProvisionerName, request.SearchScope.ClusterName, request.SearchScope.Name}
data.SetId(strings.Join(fullNameList, "/"))
}
}
case commonscope.ClusterGroupScope:
if scopedFullnameData.FullnameClusterGroup != nil {
request, err := tfModelCGDataSourceRequestConverter.ConvertTFSchemaToAPIModel(data, []string{})
if err != nil {
return diag.FromErr(errors.Wrapf(err, "Couldn't read Tanzu Mission Control backup schedule."))
}
resp, err := config.TMCConnection.ClusterGroupBackupScheduleService.VmwareTanzuManageV1alpha1ClustergroupBackupScheduleResourceServiceList(request)
switch {
case err != nil:
return diag.FromErr(errors.Wrap(err, "Couldn't list backup schedules"))
case resp.Schedules == nil:
data.SetId("NO_DATA")
default:
err = tfModelCGDataSourceResponseConverter.FillTFSchema(resp, data)
if err != nil {
diags = diag.FromErr(err)
}
fullNameList := []string{request.SearchScope.ClusterGroupName, request.SearchScope.Name}
data.SetId(strings.Join(fullNameList, "/"))
}
}
case commonscope.UnknownScope:
return diag.Errorf("no valid scope type block found: minimum one valid scope type block is required among: %v. Please check the schema.", strings.Join(scope.ScopesAllowed[:], `, `))
}
return diags
}