From 1be6c31b44e85654724a961e4274bbef1e0ac800 Mon Sep 17 00:00:00 2001 From: Dharsan Date: Tue, 16 Apr 2024 15:44:53 +0530 Subject: [PATCH] fix(keys): fixed pr issues --- docs/resources/key.md | 57 ++++++++++++++++++++ examples/resources/humanitec_key/import.sh | 1 + examples/resources/humanitec_key/resource.tf | 3 ++ internal/provider/resource_keys.go | 23 ++------ internal/provider/resource_keys_test.go | 9 +++- 5 files changed, 74 insertions(+), 19 deletions(-) create mode 100644 docs/resources/key.md create mode 100755 examples/resources/humanitec_key/import.sh create mode 100644 examples/resources/humanitec_key/resource.tf diff --git a/docs/resources/key.md b/docs/resources/key.md new file mode 100644 index 0000000..882df8b --- /dev/null +++ b/docs/resources/key.md @@ -0,0 +1,57 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "humanitec_key Resource - terraform-provider-humanitec" +subcategory: "" +description: |- + A key is used by Humanitec to ensure ensure access to Humanitec hosted drivers. The key helps Humanitec operator to establish identity against the Humanitec Driver API +--- + +# humanitec_key (Resource) + +A key is used by Humanitec to ensure ensure access to Humanitec hosted drivers. The key helps Humanitec operator to +establish identity against the Humanitec Driver API + +## Example Usage + +```terraform +resource "humanitec_key" "example" { + key = "-----BEGIN PUBLIC KEY-----\nMIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAx49tM67P+PDklVOXbbGo\nH3Z5KveonK/bjdiAXsgD8TwGG5w9cP4IRSXKFboHS16Sg4CiZOZdBuJmmfFT7VHK\ni/NIThcD0vuF8UoOQV72Fla+Qb315kWxhlxhVVd6kdqQf4SqVthzzExBMfDyYnLl\n12uFy24XVPGWp9yrFOCrI2pX9/F3aUZh4S1/vDq8pdVBaE302v31aQmMboJqgQVf\nUuvDlFsBPnzvjPjVZhlI/pAP6qfySJ2P6yU6RKCE2HlYtGs499Hvuy+GZTBzd/9+\nsZBqJHwtG2Qwh9vu8PNKUqmAqiSOoOKX4H0xz3Nj4SD/6/qPiCW0e/M2Ws/hXJSv\ntTLud8KNHP6u7aNPYg+V/l6cWcsFr/ZOoMMhqzkEOtKaxCH9c0NqCBv7QxxzF5Md\nt2oHyGrg1QiZd4U2BgWToMbyEaUKJ4G0nFPKYfZh7Udcrt7Vpgpci7jd2W73oWzS\nVhaEyCWgZRnZXXicgT8R55OQdSPXyZcLg57tBP4oursMHGYteSOYSw6nOpc+npW+\nishTpHN52g+z0GLsP7YHZ4oggveKK/7ZNUgBLrJrbhBmPsU/xNqu2jewfC3rEO1X\nbIyD6471lEhdiooy8piRl05vv5uJb3A+vPVvHt6l2koCqKGKOYnfY/okxV7rVD0i\ncOVo7D7KNwPy+CNwZIEDJAcCAwEAAQ==\n-----END PUBLIC KEY-----\n" +} +``` + + +## Schema + +### Required + +- `key` (String) The public key that is used for authentication. + +### Optional + +- `timeouts` (Attributes) (see [below for nested schema](#nestedatt--timeouts)) + +### Read-Only + +- `fingerprint` (String) Hexadecimal representation of the SHA256 hash of the DER representation of the key. +- `id` (String) The ID which refers to a specific key. + + +### Nested Schema for `timeouts` + +Optional: + +- `delete` (String) A string that can be [parsed as a duration](https://pkg.go.dev/time#ParseDuration) consisting of + numbers and unit suffixes, such as "30s" or "2h45m". Valid time units are "s" (seconds), "m" (minutes), "h" (hours). + Setting a timeout for a Delete operation is only applicable if changes are saved into state before the destroy + operation occurs. +- `read` (String) A string that can be [parsed as a duration](https://pkg.go.dev/time#ParseDuration) consisting of + numbers and unit suffixes, such as "30s" or "2h45m". Valid time units are "s" (seconds), "m" (minutes), "h" (hours). + Read operations occur during any refresh or planning operation when refresh is enabled. + +## Import + +Import is supported using the following syntax: + +```shell +terraform import humanitec_key.example key_id +``` diff --git a/examples/resources/humanitec_key/import.sh b/examples/resources/humanitec_key/import.sh new file mode 100755 index 0000000..aadb666 --- /dev/null +++ b/examples/resources/humanitec_key/import.sh @@ -0,0 +1 @@ +terraform import humanitec_key.example key_id diff --git a/examples/resources/humanitec_key/resource.tf b/examples/resources/humanitec_key/resource.tf new file mode 100644 index 0000000..1268fd1 --- /dev/null +++ b/examples/resources/humanitec_key/resource.tf @@ -0,0 +1,3 @@ +resource "humanitec_key" "example" { + key = "-----BEGIN PUBLIC KEY-----\nMIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAx49tM67P+PDklVOXbbGo\nH3Z5KveonK/bjdiAXsgD8TwGG5w9cP4IRSXKFboHS16Sg4CiZOZdBuJmmfFT7VHK\ni/NIThcD0vuF8UoOQV72Fla+Qb315kWxhlxhVVd6kdqQf4SqVthzzExBMfDyYnLl\n12uFy24XVPGWp9yrFOCrI2pX9/F3aUZh4S1/vDq8pdVBaE302v31aQmMboJqgQVf\nUuvDlFsBPnzvjPjVZhlI/pAP6qfySJ2P6yU6RKCE2HlYtGs499Hvuy+GZTBzd/9+\nsZBqJHwtG2Qwh9vu8PNKUqmAqiSOoOKX4H0xz3Nj4SD/6/qPiCW0e/M2Ws/hXJSv\ntTLud8KNHP6u7aNPYg+V/l6cWcsFr/ZOoMMhqzkEOtKaxCH9c0NqCBv7QxxzF5Md\nt2oHyGrg1QiZd4U2BgWToMbyEaUKJ4G0nFPKYfZh7Udcrt7Vpgpci7jd2W73oWzS\nVhaEyCWgZRnZXXicgT8R55OQdSPXyZcLg57tBP4oursMHGYteSOYSw6nOpc+npW+\nishTpHN52g+z0GLsP7YHZ4oggveKK/7ZNUgBLrJrbhBmPsU/xNqu2jewfC3rEO1X\nbIyD6471lEhdiooy8piRl05vv5uJb3A+vPVvHt6l2koCqKGKOYnfY/okxV7rVD0i\ncOVo7D7KNwPy+CNwZIEDJAcCAwEAAQ==\n-----END PUBLIC KEY-----\n" +} diff --git a/internal/provider/resource_keys.go b/internal/provider/resource_keys.go index 1f83df1..2877107 100644 --- a/internal/provider/resource_keys.go +++ b/internal/provider/resource_keys.go @@ -9,6 +9,8 @@ import ( "github.com/hashicorp/terraform-plugin-framework/path" "github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/resource/schema" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" "github.com/hashicorp/terraform-plugin-framework/types" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry" @@ -37,9 +39,6 @@ type ResourceKey struct { type OperatorKeyModel struct { ID types.String `tfsdk:"id"` Key types.String `tfsdk:"key"` - CreatedAt types.String `tfsdk:"created_at"` - CreatedBy types.String `tfsdk:"created_by"` - ExpiredAt types.String `tfsdk:"expired_at"` Fingerprint types.String `tfsdk:"fingerprint"` Timeouts timeouts.Value `tfsdk:"timeouts"` } @@ -60,18 +59,9 @@ func (r *ResourceKey) Schema(ctx context.Context, _ resource.SchemaRequest, resp "key": schema.StringAttribute{ MarkdownDescription: "The public key that is used for authentication.", Required: true, - }, - "created_at": schema.StringAttribute{ - MarkdownDescription: "Time that the key was created.", - Computed: true, - }, - "created_by": schema.StringAttribute{ - MarkdownDescription: "The ID of the user who created the key.", - Computed: true, - }, - "expired_at": schema.StringAttribute{ - MarkdownDescription: "Date time of the key expiration.", - Computed: true, + PlanModifiers: []planmodifier.String{ + stringplanmodifier.RequiresReplace(), + }, }, "fingerprint": schema.StringAttribute{ MarkdownDescription: "Hexadecimal representation of the SHA256 hash of the DER representation of the key.", @@ -109,9 +99,6 @@ func (r *ResourceKey) Configure(ctx context.Context, req resource.ConfigureReque func parseKeysResponse(res *client.PublicKey, data *OperatorKeyModel) { data.ID = types.StringValue(res.Id) data.Key = types.StringValue(res.Key) - data.CreatedAt = types.StringValue(res.CreatedAt.String()) - data.CreatedBy = types.StringValue(res.CreatedBy) - data.ExpiredAt = types.StringValue(res.ExpiredAt.String()) data.Fingerprint = types.StringValue(res.Fingerprint) } diff --git a/internal/provider/resource_keys_test.go b/internal/provider/resource_keys_test.go index ad2ed0d..3df7091 100644 --- a/internal/provider/resource_keys_test.go +++ b/internal/provider/resource_keys_test.go @@ -10,6 +10,7 @@ import ( func TestAccResourceKeys(t *testing.T) { key := getPublicKey(t) + var id string resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, @@ -19,6 +20,8 @@ func TestAccResourceKeys(t *testing.T) { Config: testAccResourceKey(key), Check: resource.ComposeAggregateTestCheckFunc( resource.TestCheckResourceAttr("humanitec_key.key_test", "key", key), + resource.TestCheckResourceAttrSet("humanitec_key.key_test", "id"), + resource.TestCheckResourceAttrSet("humanitec_key.key_test", "fingerprint"), ), }, // ImportState testing @@ -27,9 +30,13 @@ func TestAccResourceKeys(t *testing.T) { ImportState: true, ImportStateVerify: true, ImportStateIdFunc: func(s *terraform.State) (string, error) { - id := s.RootModule().Resources["humanitec_key.key_test"].Primary.Attributes["id"] + id = s.RootModule().Resources["humanitec_key.key_test"].Primary.Attributes["id"] return id, nil }, + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr("humanitec_key.key_test", "key", key), + resource.TestCheckResourceAttr("humanitec_key.key_test", "id", id), + ), }, // Delete testing automatically occurs in TestCase },