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.
Add zentral_mdm_push_certificate data source
- Loading branch information
Showing
6 changed files
with
246 additions
and
3 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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package provider | ||
|
||
import ( | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
"github.com/zentralopensource/goztl" | ||
) | ||
|
||
type mdmPushCertificate struct { | ||
ID types.Int64 `tfsdk:"id"` | ||
ProvisioningUID types.String `tfsdk:"provisioning_uid"` | ||
Name types.String `tfsdk:"name"` | ||
Topic types.String `tfsdk:"topic"` | ||
Certificate types.String `tfsdk:"certificate"` | ||
} | ||
|
||
func mdmPushCertificateForState(mpc *goztl.MDMPushCertificate) mdmPushCertificate { | ||
var provisioningUID types.String | ||
if mpc.ProvisioningUID != nil { | ||
provisioningUID = types.StringValue(*mpc.ProvisioningUID) | ||
} else { | ||
provisioningUID = types.StringNull() | ||
} | ||
|
||
var topic types.String | ||
if mpc.Topic != nil { | ||
topic = types.StringValue(*mpc.Topic) | ||
} else { | ||
topic = types.StringNull() | ||
} | ||
|
||
var certificate types.String | ||
if mpc.Certificate != nil { | ||
certificate = types.StringValue(*mpc.Certificate) | ||
} else { | ||
certificate = types.StringNull() | ||
} | ||
|
||
return mdmPushCertificate{ | ||
ID: types.Int64Value(int64(mpc.ID)), | ||
ProvisioningUID: provisioningUID, | ||
Name: types.StringValue(mpc.Name), | ||
Topic: topic, | ||
Certificate: certificate, | ||
} | ||
} |
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,137 @@ | ||
package provider | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/datasource" | ||
"github.com/hashicorp/terraform-plugin-framework/datasource/schema" | ||
"github.com/zentralopensource/goztl" | ||
) | ||
|
||
// Ensure provider defined types fully satisfy framework interfaces | ||
var _ datasource.DataSource = &MDMPushCertificateDataSource{} | ||
|
||
func NewMDMPushCertificateDataSource() datasource.DataSource { | ||
return &MDMPushCertificateDataSource{} | ||
} | ||
|
||
// MDMPushCertificateDataSource defines the data source implementation. | ||
type MDMPushCertificateDataSource struct { | ||
client *goztl.Client | ||
} | ||
|
||
func (d *MDMPushCertificateDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { | ||
resp.TypeName = req.ProviderTypeName + "_mdm_push_certificate" | ||
} | ||
|
||
func (d *MDMPushCertificateDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { | ||
resp.Schema = schema.Schema{ | ||
Description: "Allows details of a MDM push certificate to be retrieved by its ID or its name.", | ||
MarkdownDescription: "The data source `zentral_mdm_push_certificate` allows details of a MDM push certificate to be retrieved by its `ID` or its `name`.", | ||
|
||
Attributes: map[string]schema.Attribute{ | ||
"id": schema.Int64Attribute{ | ||
Description: "ID of the MDM push certificate.", | ||
MarkdownDescription: "`ID` of the MDM push certificate.", | ||
Optional: true, | ||
}, | ||
"provisioning_uid": schema.StringAttribute{ | ||
Description: "Provisioning UID of the push certificate.", | ||
MarkdownDescription: "Provisioning `UID` of the push certificate.", | ||
Computed: true, | ||
}, | ||
"name": schema.StringAttribute{ | ||
Description: "Name of the push certificate.", | ||
MarkdownDescription: "Name of the push certificate.", | ||
Optional: true, | ||
}, | ||
"topic": schema.StringAttribute{ | ||
Description: "APNS topic the push certificate.", | ||
MarkdownDescription: "APNS topic of the push certificate.", | ||
Computed: true, | ||
}, | ||
"certificate": schema.StringAttribute{ | ||
Description: "Push certificate in PEM form.", | ||
MarkdownDescription: "Push certificate in `PEM` form.", | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (d *MDMPushCertificateDataSource) 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.(*goztl.Client) | ||
|
||
if !ok { | ||
resp.Diagnostics.AddError( | ||
"Unexpected Data Source Configure Type", | ||
fmt.Sprintf("Expected *goztl.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData), | ||
) | ||
|
||
return | ||
} | ||
|
||
d.client = client | ||
} | ||
|
||
func (d *MDMPushCertificateDataSource) ValidateConfig(ctx context.Context, req datasource.ValidateConfigRequest, resp *datasource.ValidateConfigResponse) { | ||
var data mdmPushCertificate | ||
diags := req.Config.Get(ctx, &data) | ||
resp.Diagnostics.Append(diags...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
if data.ID.IsNull() && data.Name.IsNull() { | ||
resp.Diagnostics.AddError( | ||
"Invalid `zentral_mdm_push_certificate` data source", | ||
"`id` or `name` missing", | ||
) | ||
} else if !data.ID.IsNull() && !data.Name.IsNull() { | ||
resp.Diagnostics.AddError( | ||
"Invalid `zentral_mdm_push_certificate` data source", | ||
"`id` and `name` cannot be both set", | ||
) | ||
} | ||
} | ||
|
||
func (d *MDMPushCertificateDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { | ||
var data mdmPushCertificate | ||
|
||
// Read Terraform push certificate data into the model | ||
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) | ||
|
||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
var ztlMPC *goztl.MDMPushCertificate | ||
var err error | ||
if data.ID.ValueInt64() > 0 { | ||
ztlMPC, _, err = d.client.MDMPushCertificates.GetByID(ctx, int(data.ID.ValueInt64())) | ||
if err != nil { | ||
resp.Diagnostics.AddError( | ||
"Client Error", | ||
fmt.Sprintf("Unable to get MDM push certificate '%d' by ID, got error: %s", data.ID.ValueInt64(), err), | ||
) | ||
} | ||
} else { | ||
ztlMPC, _, err = d.client.MDMPushCertificates.GetByName(ctx, data.Name.ValueString()) | ||
if err != nil { | ||
resp.Diagnostics.AddError( | ||
"Client Error", | ||
fmt.Sprintf("Unable to get MDM push certificate '%s' by name, got error: %s", data.Name.ValueString(), err), | ||
) | ||
} | ||
} | ||
|
||
if ztlMPC != nil { | ||
resp.Diagnostics.Append(resp.State.Set(ctx, mdmPushCertificateForState(ztlMPC))...) | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
internal/provider/mdm_push_certificate_data_source_test.go
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,60 @@ | ||
package provider | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
func TestAccMDMPushCertificateDataSource(t *testing.T) { | ||
ds1ResourceName := "data.zentral_mdm_push_certificate.check1_by_name" | ||
ds2ResourceName := "data.zentral_mdm_push_certificate.check2_by_id" | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccMDMPushCertificateDataSourceConfig(), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
// Read by name | ||
resource.TestCheckResourceAttr( | ||
ds1ResourceName, "id", "4"), | ||
resource.TestCheckResourceAttr( | ||
ds1ResourceName, "provisioning_uid", "TF provider GitHub"), | ||
resource.TestCheckResourceAttr( | ||
ds1ResourceName, "name", "TF provider GitHub"), | ||
resource.TestCheckNoResourceAttr( | ||
ds1ResourceName, "topic"), | ||
resource.TestCheckNoResourceAttr( | ||
ds1ResourceName, "certificate"), | ||
// Read by ID | ||
resource.TestCheckResourceAttr( | ||
ds2ResourceName, "id", "4"), | ||
resource.TestCheckResourceAttr( | ||
ds2ResourceName, "provisioning_uid", "TF provider GitHub"), | ||
resource.TestCheckResourceAttr( | ||
ds2ResourceName, "name", "TF provider GitHub"), | ||
resource.TestCheckNoResourceAttr( | ||
ds2ResourceName, "topic"), | ||
resource.TestCheckNoResourceAttr( | ||
ds2ResourceName, "certificate"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
// TODO: hard coded values of a provisioned push certificate | ||
// on the server used for the integration tests | ||
func testAccMDMPushCertificateDataSourceConfig() string { | ||
return ` | ||
data "zentral_mdm_push_certificate" "check1_by_name" { | ||
name = "TF provider GitHub" | ||
} | ||
data "zentral_mdm_push_certificate" "check2_by_id" { | ||
id = 4 | ||
} | ||
` | ||
} |
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