/
data_source_offer.go
145 lines (124 loc) · 4.52 KB
/
data_source_offer.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
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright 2023 Canonical Ltd.
// Licensed under the Apache License, Version 2.0, see LICENCE file for details.
package provider
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/juju/terraform-provider-juju/internal/juju"
)
// Ensure provider defined types fully satisfy framework interfaces.
var _ datasource.DataSourceWithConfigure = &offerDataSource{}
func NewOfferDataSource() datasource.DataSource {
return &offerDataSource{}
}
type offerDataSource struct {
client *juju.Client
// context for the logging subsystem.
subCtx context.Context
}
// offerDataSourceModel is the juju data stored by terraform.
// tfsdk must match offer data source schema attribute names.
type offerDataSourceModel struct {
ApplicationName types.String `tfsdk:"application_name"`
Endpoint types.String `tfsdk:"endpoint"`
ModelName types.String `tfsdk:"model"`
OfferName types.String `tfsdk:"name"`
OfferURL types.String `tfsdk:"url"`
// ID required by the testing framework
ID types.String `tfsdk:"id"`
}
func (d *offerDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_offer"
}
func (d *offerDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "A data source representing a Juju Offer.",
Attributes: map[string]schema.Attribute{
"url": schema.StringAttribute{
Description: "The offer URL.",
Required: true,
},
"model": schema.StringAttribute{
Description: "The name of the model to operate in.",
Computed: true,
// TODO hml 21-aug-2023
// Is model necessary at all?
},
"name": schema.StringAttribute{
Description: "The name of the offer.",
Computed: true,
},
"application_name": schema.StringAttribute{
Description: "The name of the application.",
Computed: true,
},
"endpoint": schema.StringAttribute{
Description: "The endpoint name.",
Computed: true,
},
// ID required by the testing framework
"id": schema.StringAttribute{
Computed: true,
},
},
}
}
func (d *offerDataSource) 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.(*juju.Client)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected *http.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}
d.client = client
d.subCtx = tflog.NewSubsystem(ctx, LogDataSourceOffer)
}
func (d *offerDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
// Prevent panic if the provider has not been configured.
if d.client == nil {
addDSClientNotConfiguredError(&resp.Diagnostics, "offer")
return
}
var data offerDataSourceModel
// Read Terraform configuration data into the model
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
// Get current juju machine data source values .
offer, err := d.client.Offers.ReadOffer(&juju.ReadOfferInput{
OfferURL: data.OfferURL.ValueString(),
})
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read offer, got error: %s", err))
return
}
d.trace(fmt.Sprintf("read juju offer %q data source", data.OfferName))
// Save data into Terraform state
data.ApplicationName = types.StringValue(offer.ApplicationName)
data.Endpoint = types.StringValue(offer.Endpoint)
data.ModelName = types.StringValue(offer.ModelName)
data.OfferName = types.StringValue(offer.Name)
data.OfferURL = types.StringValue(offer.OfferURL)
data.ID = types.StringValue(offer.OfferURL)
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
func (d *offerDataSource) trace(msg string, additionalFields ...map[string]interface{}) {
if d.subCtx == nil {
return
}
//SubsystemTrace(subCtx, "datasource-offer", "hello, world", map[string]interface{}{"foo": 123})
// Output:
// {"@level":"trace","@message":"hello, world","@module":"juju.datasource-offer","foo":123}
tflog.SubsystemTrace(d.subCtx, LogDataSourceOffer, msg, additionalFields...)
}