generated from hashicorp/terraform-provider-scaffolding-framework
-
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.
- Loading branch information
Showing
19 changed files
with
1,166 additions
and
153 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
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,115 @@ | ||
package provider | ||
|
||
import ( | ||
"context" | ||
"sort" | ||
|
||
"github.com/cdn77/cdn77-client-go" | ||
"github.com/cdn77/terraform-provider-cdn77/internal/util" | ||
"github.com/hashicorp/terraform-plugin-framework/datasource" | ||
"github.com/hashicorp/terraform-plugin-framework/datasource/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
) | ||
|
||
type ObjectStoragesModel struct { | ||
Clusters []ObjectStorageModel `tfsdk:"clusters"` | ||
} | ||
|
||
type ObjectStorageModel struct { | ||
Id types.String `tfsdk:"id"` | ||
Host types.String `tfsdk:"host"` | ||
Label types.String `tfsdk:"label"` | ||
Port types.Int64 `tfsdk:"port"` | ||
Scheme types.String `tfsdk:"scheme"` | ||
} | ||
|
||
var _ datasource.DataSourceWithConfigure = &ObjectStoragesDataSource{} | ||
|
||
func NewObjectStoragesDataSource() datasource.DataSource { | ||
return &ObjectStoragesDataSource{} | ||
} | ||
|
||
type ObjectStoragesDataSource struct { | ||
client cdn77.ClientWithResponsesInterface | ||
} | ||
|
||
func (*ObjectStoragesDataSource) Metadata( | ||
_ context.Context, | ||
req datasource.MetadataRequest, | ||
resp *datasource.MetadataResponse, | ||
) { | ||
resp.TypeName = req.ProviderTypeName + "_object_storages" | ||
} | ||
|
||
func (*ObjectStoragesDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { | ||
resp.Schema = schema.Schema{ | ||
Attributes: map[string]schema.Attribute{ | ||
"clusters": schema.ListNestedAttribute{ | ||
NestedObject: schema.NestedAttributeObject{ | ||
Attributes: map[string]schema.Attribute{ | ||
"id": schema.StringAttribute{ | ||
Description: "ID (UUID) of the Object Storage cluster", | ||
Computed: true, | ||
}, | ||
"host": schema.StringAttribute{ | ||
Computed: true, | ||
}, | ||
"label": schema.StringAttribute{ | ||
Computed: true, | ||
}, | ||
"port": schema.Int64Attribute{ | ||
Computed: true, | ||
}, | ||
"scheme": schema.StringAttribute{ | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
Computed: true, | ||
Description: "List of all Object Storage clusters", | ||
}, | ||
}, | ||
Description: "Object Storages data source allows you to read all available Object Storage clusters", | ||
} | ||
} | ||
|
||
func (d *ObjectStoragesDataSource) Configure( | ||
_ context.Context, | ||
req datasource.ConfigureRequest, | ||
resp *datasource.ConfigureResponse, | ||
) { | ||
resp.Diagnostics.Append(util.MaybeSetClient(req.ProviderData, &d.client)) | ||
} | ||
|
||
func (d *ObjectStoragesDataSource) Read(ctx context.Context, _ datasource.ReadRequest, resp *datasource.ReadResponse) { | ||
const errMessage = "Failed to fetch list of all Object Storages" | ||
|
||
response, err := d.client.ObjectStorageClusterListWithResponse(ctx) | ||
if err != nil { | ||
resp.Diagnostics.AddError(errMessage, err.Error()) | ||
|
||
return | ||
} | ||
|
||
if !util.CheckResponse(&resp.Diagnostics, errMessage, response, response.JSONDefault) { | ||
return | ||
} | ||
|
||
objectStorages := make([]ObjectStorageModel, 0, len(*response.JSON200)) | ||
|
||
for _, objectStorage := range *response.JSON200 { | ||
objectStorages = append(objectStorages, ObjectStorageModel{ | ||
Id: types.StringValue(objectStorage.Id), | ||
Host: types.StringValue(objectStorage.Host), | ||
Label: types.StringValue(objectStorage.Label), | ||
Port: util.IntPointerToInt64Value(objectStorage.Port), | ||
Scheme: types.StringValue(objectStorage.Scheme), | ||
}) | ||
} | ||
|
||
sort.SliceStable(objectStorages, func(i, j int) bool { | ||
return objectStorages[i].Id.ValueString() < objectStorages[j].Id.ValueString() | ||
}) | ||
|
||
resp.Diagnostics.Append(resp.State.Set(ctx, &ObjectStoragesModel{Clusters: objectStorages})...) | ||
} |
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,46 @@ | ||
package provider_test | ||
|
||
import ( | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/cdn77/terraform-provider-cdn77/internal/acctest" | ||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-testing/knownvalue" | ||
"github.com/hashicorp/terraform-plugin-testing/statecheck" | ||
"github.com/hashicorp/terraform-plugin-testing/tfjsonpath" | ||
) | ||
|
||
func TestAccObjectStoragesDataSource_All(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
ProtoV6ProviderFactories: acctest.GetProviderFactories(), | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: objectStoragesDataSourceConfig, | ||
ConfigStateChecks: []statecheck.StateCheck{ | ||
statecheck.ExpectKnownValue("data.cdn77_object_storages.all", | ||
tfjsonpath.New("clusters"), | ||
knownvalue.SetPartial([]knownvalue.Check{ | ||
knownvalue.ObjectExact(map[string]knownvalue.Check{ | ||
"id": knownvalue.StringRegexp(regexp.MustCompile( | ||
`^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$`, | ||
)), | ||
"host": knownvalue.StringRegexp(regexp.MustCompile( | ||
`^([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}$`, | ||
)), | ||
"label": knownvalue.StringExact("EU"), | ||
"port": knownvalue.Int64Exact(443), | ||
"scheme": knownvalue.StringExact("https"), | ||
}), | ||
}), | ||
), | ||
}, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
const objectStoragesDataSourceConfig = ` | ||
data "cdn77_object_storages" "all" { | ||
} | ||
` |
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,136 @@ | ||
package provider | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag" | ||
"github.com/hashicorp/terraform-plugin-framework/attr" | ||
"github.com/hashicorp/terraform-plugin-framework/datasource" | ||
"github.com/hashicorp/terraform-plugin-framework/diag" | ||
"github.com/hashicorp/terraform-plugin-framework/path" | ||
"github.com/hashicorp/terraform-plugin-framework/resource" | ||
"github.com/hashicorp/terraform-plugin-framework/tfsdk" | ||
) | ||
|
||
var ( | ||
_ datasource.ConfigValidator = &OriginConfigValidator{} | ||
_ resource.ConfigValidator = &OriginConfigValidator{} | ||
) | ||
|
||
type OriginConfigValidator struct{} | ||
|
||
func NewOriginTypeConfigValidator() *OriginConfigValidator { | ||
return &OriginConfigValidator{} | ||
} | ||
|
||
func (v OriginConfigValidator) Description(ctx context.Context) string { | ||
return v.MarkdownDescription(ctx) | ||
} | ||
|
||
func (OriginConfigValidator) MarkdownDescription(_ context.Context) string { | ||
return "Checks Origin configuration for all required/conflicting attributes" | ||
} | ||
|
||
func (v OriginConfigValidator) ValidateDataSource( | ||
ctx context.Context, | ||
req datasource.ValidateConfigRequest, | ||
resp *datasource.ValidateConfigResponse, | ||
) { | ||
resp.Diagnostics = v.Validate(ctx, req.Config) | ||
} | ||
|
||
func (v OriginConfigValidator) ValidateResource( | ||
ctx context.Context, | ||
req resource.ValidateConfigRequest, | ||
resp *resource.ValidateConfigResponse, | ||
) { | ||
resp.Diagnostics = v.Validate(ctx, req.Config) | ||
} | ||
|
||
func (v OriginConfigValidator) Validate(ctx context.Context, config tfsdk.Config) diag.Diagnostics { | ||
var data OriginModel | ||
diags := config.Get(ctx, &data) | ||
|
||
originType := data.Type.ValueString() | ||
awsAttributes := []attrNameAndValue{ | ||
{name: "aws_access_key_id", value: data.AwsAccessKeyId}, | ||
{name: "aws_access_key_secret", value: data.AwsAccessKeySecret}, | ||
{name: "aws_region", value: data.AwsRegion}, | ||
} | ||
objectStorageAttributes := []attrNameAndValue{ | ||
{name: "acl", value: data.Acl}, | ||
{name: "cluster_id", value: data.ClusterId}, | ||
//{name: "access_key_id", value: data.AccessKeyId}, | ||
//{name: "access_key_secret", value: data.AccessKeySecret}, | ||
{name: "bucket_name", value: data.BucketName}, | ||
} | ||
schemeAndHostAttributes := []attrNameAndValue{ | ||
{name: "scheme", value: data.Scheme}, | ||
{name: "host", value: data.Host}, | ||
} | ||
var conflictingAttributes, requiredAttributes []attrNameAndValue | ||
|
||
switch originType { | ||
case OriginTypeAws: | ||
conflictingAttributes = append(conflictingAttributes, objectStorageAttributes...) | ||
requiredAttributes = schemeAndHostAttributes | ||
case OriginTypeObjectStorage: | ||
conflictingAttributes = append(conflictingAttributes, awsAttributes...) | ||
conflictingAttributes = append(conflictingAttributes, schemeAndHostAttributes...) | ||
conflictingAttributes = append(conflictingAttributes, attrNameAndValue{name: "port", value: data.Port}) | ||
conflictingAttributes = append(conflictingAttributes, attrNameAndValue{name: "base_dir", value: data.BaseDir}) | ||
requiredAttributes = objectStorageAttributes | ||
case OriginTypeUrl: | ||
conflictingAttributes = append(conflictingAttributes, awsAttributes...) | ||
conflictingAttributes = append(conflictingAttributes, objectStorageAttributes...) | ||
requiredAttributes = schemeAndHostAttributes | ||
default: | ||
addUnknownOriginTypeError(&diags, data) | ||
|
||
return diags | ||
} | ||
|
||
diags.Append(v.doValidate(originType, conflictingAttributes, requiredAttributes)...) | ||
|
||
return diags | ||
} | ||
|
||
func (OriginConfigValidator) doValidate( | ||
originType string, | ||
conflictingAttributes []attrNameAndValue, | ||
requiredAttributes []attrNameAndValue, | ||
) (diags diag.Diagnostics) { | ||
for _, attribute := range conflictingAttributes { | ||
if attribute.value.IsNull() { | ||
continue | ||
} | ||
|
||
diags.Append( | ||
validatordiag.InvalidAttributeCombinationDiagnostic( | ||
path.Root(attribute.name), | ||
fmt.Sprintf(`Attribute "%s" can't be used with Origin type "%s"'`, attribute.name, originType), | ||
), | ||
) | ||
} | ||
|
||
for _, attribute := range requiredAttributes { | ||
if !attribute.value.IsNull() { | ||
continue | ||
} | ||
|
||
diags.Append( | ||
validatordiag.InvalidAttributeCombinationDiagnostic( | ||
path.Root(attribute.name), | ||
fmt.Sprintf(`Attribute "%s" is required for Origin type "%s"'`, attribute.name, originType), | ||
), | ||
) | ||
} | ||
|
||
return diags | ||
} | ||
|
||
type attrNameAndValue struct { | ||
name string | ||
value attr.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.