/
actions.go
62 lines (56 loc) · 2.11 KB
/
actions.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
package credential
import (
"context"
"net/http"
"github.com/SchwarzIT/community-stackit-go-client/pkg/validate"
"github.com/SchwarzIT/terraform-provider-stackit/stackit/internal/common"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/types"
)
// Read - lifecycle function
func (d *DataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var config Credential
diags := req.Config.Get(ctx, &config)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
res, err := d.client.Credentials.GetCredentialByID(ctx, config.ProjectID.ValueString(), config.InstanceID.ValueString(), config.ID.ValueString())
if agg := common.Validate(&resp.Diagnostics, res, err, "JSON200.Raw"); agg != nil {
if validate.StatusEquals(res, http.StatusNotFound) {
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("failed to read credential", agg.Error())
return
}
i := res.JSON200
// set computed fields
config.Host = types.StringValue(i.Raw.Credentials.Host)
config.Hosts = types.ListNull(types.StringType)
if i.Raw.Credentials.Hosts != nil && len(*i.Raw.Credentials.Hosts) > 0 {
h := []attr.Value{}
for _, v := range *i.Raw.Credentials.Hosts {
h = append(h, types.StringValue(v))
}
config.Hosts = types.ListValueMust(types.StringType, h)
}
config.DatabaseName = types.StringValue("")
if i.Raw.Credentials.Name != nil {
config.DatabaseName = types.StringValue(*i.Raw.Credentials.Name)
}
config.Username = types.StringValue(i.Raw.Credentials.Username)
config.Password = types.StringValue(i.Raw.Credentials.Password)
config.Port = types.Int64Value(0)
if i.Raw.Credentials.Port != nil {
config.Port = types.Int64Value(int64(*i.Raw.Credentials.Port))
}
config.SyslogDrainURL = types.StringValue(i.Raw.SyslogDrainUrl)
config.RouteServiceURL = types.StringValue(i.Raw.RouteServiceUrl)
config.URI = types.StringValue(i.Uri)
resp.Diagnostics.Append(resp.State.Set(ctx, &config)...)
if resp.Diagnostics.HasError() {
return
}
}