-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #59 from pluralsh/marcin/prod-2668-add-plural_serv…
…ice_context-datasource feat: Add service context data source
- Loading branch information
Showing
7 changed files
with
128 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "plural_service_context Data Source - terraform-provider-plural" | ||
subcategory: "" | ||
description: |- | ||
A representation of a service context that can be reused during service deployment templating. | ||
--- | ||
|
||
# plural_service_context (Data Source) | ||
|
||
A representation of a service context that can be reused during service deployment templating. | ||
|
||
|
||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `name` (String) Human-readable name of this service context. | ||
|
||
### Read-Only | ||
|
||
- `configuration` (Map of String) | ||
- `id` (String) Internal identifier of this service context. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package datasource | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"terraform-provider-plural/internal/client" | ||
"terraform-provider-plural/internal/common" | ||
"terraform-provider-plural/internal/model" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/datasource" | ||
"github.com/hashicorp/terraform-plugin-framework/datasource/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
) | ||
|
||
func NewServiceContextDataSource() datasource.DataSource { | ||
return &serviceContextDataSource{} | ||
} | ||
|
||
type serviceContextDataSource struct { | ||
client *client.Client | ||
} | ||
|
||
func (d *serviceContextDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { | ||
resp.TypeName = req.ProviderTypeName + "_service_context" | ||
} | ||
|
||
func (d *serviceContextDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { | ||
resp.Schema = schema.Schema{ | ||
MarkdownDescription: "A representation of a service context that can be reused during service deployment templating.", | ||
Attributes: map[string]schema.Attribute{ | ||
"id": schema.StringAttribute{ | ||
Computed: true, | ||
Description: "Internal identifier of this service context.", | ||
MarkdownDescription: "Internal identifier of this service context.", | ||
}, | ||
"name": schema.StringAttribute{ | ||
Description: "Human-readable name of this service context.", | ||
MarkdownDescription: "Human-readable name of this service context.", | ||
Required: true, | ||
}, | ||
"configuration": schema.MapAttribute{ | ||
Description: "", | ||
MarkdownDescription: "", | ||
Computed: true, | ||
ElementType: types.StringType, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (d *serviceContextDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { | ||
if req.ProviderData == nil { | ||
return | ||
} | ||
|
||
data, ok := req.ProviderData.(*common.ProviderData) | ||
if !ok { | ||
resp.Diagnostics.AddError( | ||
"Unexpected Service Context Data Source Configure Type", | ||
fmt.Sprintf("Expected *common.ProviderData, got: %T. Please report this issue to the provider developers.", req.ProviderData), | ||
) | ||
return | ||
} | ||
|
||
d.client = data.Client | ||
} | ||
|
||
func (d *serviceContextDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { | ||
data := new(model.ServiceContext) | ||
resp.Diagnostics.Append(req.Config.Get(ctx, data)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
response, err := d.client.GetServiceContext(ctx, data.Name.ValueString()) | ||
if err != nil { | ||
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read service context, got error: %s", err)) | ||
return | ||
} | ||
|
||
data.From(response.ServiceContext, ctx, resp.Diagnostics) | ||
resp.Diagnostics.Append(resp.State.Set(ctx, data)...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters