/
data_source_mobility_group_instance.go
102 lines (81 loc) · 3.4 KB
/
data_source_mobility_group_instance.go
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
/*
Copyright (c) 2024 Dell Inc., or its subsidiaries. All Rights Reserved.
Licensed under the Mozilla Public License Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://mozilla.org/MPL/2.0/
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package navigator //nolint:dupl
import (
"context"
"fmt"
"net/http"
client "github.com/dell/terraform-provider-apex/client/apexclient/client"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/types"
)
// Ensure the implementation satisfies the expected interfaces.
var (
_ datasource.DataSource = &mobilityGroupInstanceDataSource{}
_ datasource.DataSourceWithConfigure = &mobilityGroupInstanceDataSource{}
)
// NewMobilityGroupInstanceDataSource is a storage system data source object
func NewMobilityGroupInstanceDataSource() datasource.DataSource {
return &mobilityGroupInstanceDataSource{}
}
// mobilityGroupInstanceDataSource is the data source implementation.
type mobilityGroupInstanceDataSource struct {
client *client.APIClient
}
func (d *mobilityGroupInstanceDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_mobility_group"
}
// Schema defines the acceptable configuration and state attribute names and types.
func (d *mobilityGroupInstanceDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { // nolint:funlen, dupl
resp.Schema = MobilityGroupsDataSourceSchema
}
// Read method is used to refresh the Terraform state based on the schema data.
func (d *mobilityGroupInstanceDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { // nolint:gocognit, funlen
var state MobilityGroupModel
resp.Diagnostics.Append(req.Config.Get(ctx, &state)...)
mobilityGroup, status, err := d.client.MobilityGroupsAPI.MobilityGroupsInstance(context.Background(), state.ID.ValueString()).Execute()
if (err != nil) || (status.StatusCode != http.StatusOK) {
resp.Diagnostics.AddError(
"Unable to Read Apex Navigator Mobility Groups",
err.Error(),
)
return
}
if err := status.Body.Close(); err != nil {
fmt.Print("Error Closing response body:", err)
}
// Map response body to model
mobilityGroupsState := GetMobilityGroupModel(*mobilityGroup)
state.ID = types.StringValue("placeholder")
// Set state
diags := resp.State.Set(ctx, &mobilityGroupsState)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
// Configure adds the provider configured client to the data source.
func (d *mobilityGroupInstanceDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
client, ok := req.ProviderData.(*client.APIClient)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected *openapi.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}
d.client = client
}