-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add tier datasource, datasource filter, id string validator, tests (#219
) * add tier datasource, datasource filter, id string validator, tests * update opslevel-go version, format hcl file * set datasource Filter value field to required * cast tier filter index int to string * fix idStringValidator validation * update description for idStringValidator
- Loading branch information
1 parent
369eba1
commit d29083a
Showing
12 changed files
with
364 additions
and
279 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
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 |
---|---|---|
@@ -1,94 +1,137 @@ | ||
package opslevel | ||
|
||
// import ( | ||
// "fmt" | ||
// "strconv" | ||
|
||
// "github.com/opslevel/opslevel-go/v2024" | ||
|
||
// "github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
// ) | ||
|
||
// func datasourceTier() *schema.Resource { | ||
// return &schema.Resource{ | ||
// Read: wrap(datasourceTierRead), | ||
// Schema: map[string]*schema.Schema{ | ||
// "filter": getDatasourceFilter(true, []string{"alias", "id", "index", "name"}), | ||
// "alias": { | ||
// Type: schema.TypeString, | ||
// Computed: true, | ||
// }, | ||
// "index": { | ||
// Type: schema.TypeInt, | ||
// Computed: true, | ||
// }, | ||
// "name": { | ||
// Type: schema.TypeString, | ||
// Computed: true, | ||
// }, | ||
// }, | ||
// } | ||
// } | ||
import ( | ||
"context" | ||
"fmt" | ||
"strconv" | ||
|
||
"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/opslevel/opslevel-go/v2024" | ||
) | ||
|
||
// Ensure TierDataSource implements DataSourceWithConfigure interface | ||
var _ datasource.DataSourceWithConfigure = &TierDataSource{} | ||
|
||
func NewTierDataSource() datasource.DataSource { | ||
return &TierDataSource{} | ||
} | ||
|
||
// TierDataSource manages a Tier data source. | ||
type TierDataSource struct { | ||
CommonDataSourceClient | ||
} | ||
|
||
// TierDataSourceModel describes the data source data model. | ||
type TierDataSourceModel struct { | ||
Alias types.String `tfsdk:"alias"` | ||
Filter FilterModel `tfsdk:"filter"` | ||
Id types.String `tfsdk:"id"` | ||
Index types.Int64 `tfsdk:"index"` | ||
Name types.String `tfsdk:"name"` | ||
} | ||
|
||
func NewTierDataSourceModel(ctx context.Context, tier opslevel.Tier, filter FilterModel) TierDataSourceModel { | ||
return TierDataSourceModel{ | ||
Alias: types.StringValue(string(tier.Alias)), | ||
Filter: filter, | ||
Id: types.StringValue(string(tier.Id)), | ||
Index: types.Int64Value(int64(tier.Index)), | ||
Name: types.StringValue(string(tier.Name)), | ||
} | ||
} | ||
|
||
func (d *TierDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { | ||
resp.TypeName = req.ProviderTypeName + "_tier" | ||
} | ||
|
||
func (d *TierDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { | ||
validFieldNames := []string{"alias", "id", "index", "name"} | ||
resp.Schema = schema.Schema{ | ||
// This description is used by the documentation generator and the language server. | ||
MarkdownDescription: "Tier data source", | ||
|
||
Attributes: map[string]schema.Attribute{ | ||
"alias": schema.StringAttribute{ | ||
MarkdownDescription: "Terraform specific identifier.", | ||
Computed: true, | ||
}, | ||
"id": schema.StringAttribute{ | ||
MarkdownDescription: "Terraform specific identifier.", | ||
Computed: true, | ||
}, | ||
"index": schema.Int64Attribute{ | ||
MarkdownDescription: "Terraform specific identifier.", | ||
Computed: true, | ||
}, | ||
"name": schema.StringAttribute{ | ||
Description: "The name of the domain.", | ||
Computed: true, | ||
}, | ||
}, | ||
Blocks: map[string]schema.Block{ | ||
"filter": getDatasourceFilter(validFieldNames), | ||
}, | ||
} | ||
} | ||
|
||
func (d *TierDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { | ||
var data TierDataSourceModel | ||
|
||
// Read Terraform configuration data into the model | ||
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
tiers, err := d.client.ListTiers() | ||
if err != nil { | ||
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read tier datasource, got error: %s", err)) | ||
return | ||
} | ||
|
||
tier, err := filterTiers(tiers, data.Filter) | ||
if err != nil { | ||
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to filter tier datasource, got error: %s", err)) | ||
return | ||
} | ||
|
||
tierDataModel := NewTierDataSourceModel(ctx, *tier, data.Filter) | ||
|
||
// Save data into Terraform state | ||
tflog.Trace(ctx, "read an OpsLevel Tier data source") | ||
// resp.Diagnostics.Append(diags...) | ||
resp.Diagnostics.Append(resp.State.Set(ctx, &tierDataModel)...) | ||
} | ||
|
||
// func filterTiers(levels []opslevel.Tier, field string, value string) (*opslevel.Tier, error) { | ||
// if value == "" { | ||
// return nil, fmt.Errorf("Please provide a non-empty value for filter's value") | ||
// } | ||
|
||
// var output opslevel.Tier | ||
// found := false | ||
// for _, item := range levels { | ||
// switch field { | ||
// case "alias": | ||
// if item.Alias == value { | ||
// output = item | ||
// found = true | ||
// } | ||
// case "id": | ||
// if string(item.Id) == value { | ||
// output = item | ||
// found = true | ||
// } | ||
// case "index": | ||
// if v, err := strconv.Atoi(value); err == nil && item.Index == v { | ||
// output = item | ||
// found = true | ||
// } | ||
// case "name": | ||
// if item.Name == value { | ||
// output = item | ||
// found = true | ||
// } | ||
// } | ||
// if found { | ||
// break | ||
// } | ||
// } | ||
|
||
// if !found { | ||
// return nil, fmt.Errorf("Unable to find tier with: %s==%s", field, value) | ||
// } | ||
// return &output, nil | ||
// } | ||
|
||
// func datasourceTierRead(d *schema.ResourceData, client *opslevel.Client) error { | ||
// results, err := client.ListTiers() | ||
// if err != nil { | ||
// return err | ||
// } | ||
|
||
// field := d.Get("filter.0.field").(string) | ||
// value := d.Get("filter.0.value").(string) | ||
|
||
// item, itemErr := filterTiers(results, field, value) | ||
// if itemErr != nil { | ||
// return itemErr | ||
// } | ||
|
||
// d.SetId(string(item.Id)) | ||
// d.Set("alias", item.Alias) | ||
// d.Set("index", item.Index) | ||
// d.Set("name", item.Name) | ||
|
||
// return nil | ||
// } | ||
func filterTiers(tiers []opslevel.Tier, filter FilterModel) (*opslevel.Tier, error) { | ||
if filter.Value.Equal(types.StringValue("")) { | ||
return nil, fmt.Errorf("Please provide a non-empty value for filter's value") | ||
} | ||
for _, tier := range tiers { | ||
switch filter.Field.ValueString() { | ||
case "alias": | ||
if filter.Value.Equal(types.StringValue(tier.Alias)) { | ||
return &tier, nil | ||
} | ||
case "id": | ||
if filter.Value.Equal(types.StringValue(string(tier.Id))) { | ||
return &tier, nil | ||
} | ||
case "index": | ||
index := strconv.Itoa(int(tier.Index)) | ||
if filter.Value.Equal(types.StringValue(index)) { | ||
return &tier, nil | ||
} | ||
case "name": | ||
if filter.Value.Equal(types.StringValue(tier.Name)) { | ||
return &tier, nil | ||
} | ||
} | ||
} | ||
|
||
return nil, fmt.Errorf("Unable to find tier with: %s==%s", filter.Field, filter.Value) | ||
} |
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
Oops, something went wrong.