generated from hashicorp/terraform-provider-scaffolding-framework
/
monolith_sub_manifest_data_source.go
132 lines (112 loc) · 4.31 KB
/
monolith_sub_manifest_data_source.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package provider
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/zentralopensource/goztl"
)
// Ensure provider defined types fully satisfy framework interfaces
var _ datasource.DataSource = &MonolithSubManifestDataSource{}
func NewMonolithSubManifestDataSource() datasource.DataSource {
return &MonolithSubManifestDataSource{}
}
// MonolithSubManifestDataSource defines the data source implementation.
type MonolithSubManifestDataSource struct {
client *goztl.Client
}
func (d *MonolithSubManifestDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_monolith_sub_manifest"
}
func (d *MonolithSubManifestDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Allows details of a Monolith sub manifest to be retrieved by its ID or its name.",
MarkdownDescription: "The data source `zentral_monolith_sub_manifest` allows details of a Monolith sub manifest to be retrieved by its `ID` or its `name`.",
Attributes: map[string]schema.Attribute{
"id": schema.Int64Attribute{
Description: "ID of the sub manifest.",
MarkdownDescription: "`ID` of the sub manifest.",
Optional: true,
},
"name": schema.StringAttribute{
Description: "Name of the sub manifest.",
MarkdownDescription: "Name of the sub manifest.",
Optional: true,
},
"description": schema.StringAttribute{
Description: "Description of the sub manifest.",
MarkdownDescription: "Description of the sub manifest.",
Computed: true,
},
"meta_business_unit_id": schema.Int64Attribute{
Description: "The ID of the meta business unit this sub manifest is restricted to.",
MarkdownDescription: "The `ID` of the meta business unit this sub manifest is restricted to.",
Computed: true,
},
},
}
}
func (d *MonolithSubManifestDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
// Prevent panic if the provider has not been configured.
if req.ProviderData == nil {
return
}
client, ok := req.ProviderData.(*goztl.Client)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected *goztl.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}
d.client = client
}
func (d *MonolithSubManifestDataSource) ValidateConfig(ctx context.Context, req datasource.ValidateConfigRequest, resp *datasource.ValidateConfigResponse) {
var data monolithSubManifest
diags := req.Config.Get(ctx, &data)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
if data.ID.IsNull() && data.Name.IsNull() {
resp.Diagnostics.AddError(
"Invalid `zentral_monolith_sub_manifest` data source",
"`id` or `name` missing",
)
} else if !data.ID.IsNull() && !data.Name.IsNull() {
resp.Diagnostics.AddError(
"Invalid `zentral_monolith_sub_manifest` data source",
"`id` and `name` cannot be both set",
)
}
}
func (d *MonolithSubManifestDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data monolithSubManifest
// Read Terraform sub manifest data into the model
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
var ztlMSM *goztl.MonolithSubManifest
var err error
if data.ID.ValueInt64() > 0 {
ztlMSM, _, err = d.client.MonolithSubManifests.GetByID(ctx, int(data.ID.ValueInt64()))
if err != nil {
resp.Diagnostics.AddError(
"Client Error",
fmt.Sprintf("Unable to get Monolith sub manifest '%d' by ID, got error: %s", data.ID.ValueInt64(), err),
)
}
} else {
ztlMSM, _, err = d.client.MonolithSubManifests.GetByName(ctx, data.Name.ValueString())
if err != nil {
resp.Diagnostics.AddError(
"Client Error",
fmt.Sprintf("Unable to get Monolith sub manifest '%s' by name, got error: %s", data.Name.ValueString(), err),
)
}
}
if ztlMSM != nil {
resp.Diagnostics.Append(resp.State.Set(ctx, monolithSubManifestForState(ztlMSM))...)
}
}