|
| 1 | +// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. |
| 2 | + |
| 3 | +package zero_trust_tunnel_warp_connector_token |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "net/http" |
| 10 | + |
| 11 | + "github.com/cloudflare/cloudflare-go/v4" |
| 12 | + "github.com/cloudflare/cloudflare-go/v4/option" |
| 13 | + "github.com/cloudflare/terraform-provider-cloudflare/internal/apijson" |
| 14 | + "github.com/cloudflare/terraform-provider-cloudflare/internal/logging" |
| 15 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 16 | +) |
| 17 | + |
| 18 | +type ZeroTrustTunnelWARPConnectorTokenDataSource struct { |
| 19 | + client *cloudflare.Client |
| 20 | +} |
| 21 | + |
| 22 | +var _ datasource.DataSourceWithConfigure = (*ZeroTrustTunnelWARPConnectorTokenDataSource)(nil) |
| 23 | + |
| 24 | +func NewZeroTrustTunnelWARPConnectorTokenDataSource() datasource.DataSource { |
| 25 | + return &ZeroTrustTunnelWARPConnectorTokenDataSource{} |
| 26 | +} |
| 27 | + |
| 28 | +func (d *ZeroTrustTunnelWARPConnectorTokenDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { |
| 29 | + resp.TypeName = req.ProviderTypeName + "_zero_trust_tunnel_warp_connector_token" |
| 30 | +} |
| 31 | + |
| 32 | +func (d *ZeroTrustTunnelWARPConnectorTokenDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { |
| 33 | + if req.ProviderData == nil { |
| 34 | + return |
| 35 | + } |
| 36 | + |
| 37 | + client, ok := req.ProviderData.(*cloudflare.Client) |
| 38 | + |
| 39 | + if !ok { |
| 40 | + resp.Diagnostics.AddError( |
| 41 | + "unexpected resource configure type", |
| 42 | + fmt.Sprintf("Expected *cloudflare.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData), |
| 43 | + ) |
| 44 | + |
| 45 | + return |
| 46 | + } |
| 47 | + |
| 48 | + d.client = client |
| 49 | +} |
| 50 | + |
| 51 | +func (d *ZeroTrustTunnelWARPConnectorTokenDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { |
| 52 | + var data *ZeroTrustTunnelWARPConnectorTokenDataSourceModel |
| 53 | + |
| 54 | + resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) |
| 55 | + |
| 56 | + if resp.Diagnostics.HasError() { |
| 57 | + return |
| 58 | + } |
| 59 | + |
| 60 | + params, diags := data.toReadParams(ctx) |
| 61 | + resp.Diagnostics.Append(diags...) |
| 62 | + if resp.Diagnostics.HasError() { |
| 63 | + return |
| 64 | + } |
| 65 | + |
| 66 | + res := new(http.Response) |
| 67 | + env := ZeroTrustTunnelWARPConnectorTokenResultDataSourceEnvelope{*data} |
| 68 | + _, err := d.client.ZeroTrust.Tunnels.WARPConnector.Token.Get( |
| 69 | + ctx, |
| 70 | + data.TunnelID.ValueString(), |
| 71 | + params, |
| 72 | + option.WithResponseBodyInto(&res), |
| 73 | + option.WithMiddleware(logging.Middleware(ctx)), |
| 74 | + ) |
| 75 | + if err != nil { |
| 76 | + resp.Diagnostics.AddError("failed to make http request", err.Error()) |
| 77 | + return |
| 78 | + } |
| 79 | + bytes, _ := io.ReadAll(res.Body) |
| 80 | + err = apijson.UnmarshalComputed(bytes, &env) |
| 81 | + if err != nil { |
| 82 | + resp.Diagnostics.AddError("failed to deserialize http request", err.Error()) |
| 83 | + return |
| 84 | + } |
| 85 | + data = &env.Result |
| 86 | + |
| 87 | + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) |
| 88 | +} |
0 commit comments