Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Resource: aws_appsync_api_key #3827

Merged
merged 7 commits into from
Jul 9, 2018

Conversation

lucidprogrammer
Copy link
Contributor

Supporting the creation of appsync api key. Either a specific date or number of days.

resource "aws_appsync_graphql_api" "example" {
authentication_type = "API_KEY"
name = "example"
}

resource "aws_appsync_api_key" "ex1" {
appsync_api_id = "${aws_appsync_graphql_api.example.id}"
validity_period_days = 364
}

resource "aws_appsync_api_key" "ex2" {
appsync_api_id = "${aws_appsync_graphql_api.example.id}"
valid_till_date = "30/11/2018"
}

@ghost ghost added the size/L Managed by automation to categorize the size of a PR. label Mar 18, 2018
@lucidprogrammer lucidprogrammer changed the title [WIP]New Resource: appsync_api_key New Resource: appsync_api_key Mar 18, 2018
@ghost ghost added the size/L Managed by automation to categorize the size of a PR. label Mar 19, 2018
@bflad bflad added new-resource Introduces a new resource. service/appsync Issues and PRs that pertain to the appsync service. labels Mar 19, 2018
Copy link
Contributor

@bflad bflad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @lucidprogrammer 👋 Thanks for this contribution! I have left some initial review feedback. Please let me know if you have any questions or don't have time to implement these items. Thanks!

Type: schema.TypeString,
Required: true,
},
"valid_till_date": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few notes here:

  • We should match the AWS API and create a single attribute expires rather than provide multiple conflicting attributes for the same information (drop valid_till_date, validity_period_days, and expiry_date)
  • It should accept a RFC3339 timestamp for now and can be validated via ValidateFunc: validation.ValidateRFC3339TimeString

func resourceAwsAppsyncApiKeyRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).appsyncconn

input := &appsync.ListApiKeysInput{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to account for NextToken when the list is larger than the returned results or we could potentially miss fetching the API key.

if err != nil {
return err
}
var key appsync.ApiKey
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is not accounting for if the key is not found in the ListApiKeys output(s). I would recommend changing this to var key *appsync.ApiKey and performing a nil check before dereferencing the values (otherwise it will cause a Terraform panic):

// After exhausting all ListApiKeys searching
if key == nil {
  log.Printf("[WARN] AppSync API Key %q not found, removing from state", d.Id())
  d.SetId("")
  return nil
}
d.Set("key", key.Id)
// ...

conn := meta.(*AWSClient).appsyncconn

params := &appsync.UpdateApiKeyInput{
ApiId: aws.String(d.Get("appsync_api_id").(string)),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given the API key seems a subset of the API ID, we should probably add this ID to the Terraform resource ID.

In the Create function (if : is a safe delimiter):

d.SetId(fmt.Sprintf("%s:%s"), d.Get("appsync_api_id").(string), *resp.ApiKey.Id)

We can then parse it with a helper function, e.g.

func decodeAppSyncApiKeyId(id string) (string, string, error) {
	parts := strings.Split(id, ":")
	if len(parts) != 2 {
		return "", "", fmt.Errorf("Unexpected format of ID (%q), expected API-ID:API-KEY-ID", id)
	}
	return parts[0], parts[1], nil
}

And anywhere necessary to call that:

apiID, apiKeyID, err := decodeAppSyncApiKeyId(d.Id())
if err != nil {
	return err
}

This will allow us to support importing easily with:

Importer: &schema.ResourceImporter{
	State: schema.ImportStatePassthrough,
},

Optional: true,
Default: "Managed by Terraform",
},
"appsync_api_id": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to prepend appsync_ to this attribute name. 👍

@ghost ghost added the size/L Managed by automation to categorize the size of a PR. label Mar 21, 2018
@radeksimko radeksimko changed the title New Resource: appsync_api_key New Resource: aws_appsync_api_key Mar 23, 2018
@lucidprogrammer
Copy link
Contributor Author

@bflad I will update docs tomorrow related to the resource changes later tonight

@ghost ghost added the size/L Managed by automation to categorize the size of a PR. label Mar 24, 2018
@lucidprogrammer
Copy link
Contributor Author

@bflad anything else to be done on this, let me know your thoughts. tks

@lucidprogrammer
Copy link
Contributor Author

@bflad i know it could be hectic with lot going on at your end. doing this for a customer, and pressure is mounting, where other resources for appsync also to be created.(like schema creation, types etc). any feedback to merge this will be much appreciated. tks

Copy link
Contributor

@bflad bflad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is almost there, just two more things and should be good to go. 👍

if er != nil {
return er
}
listKeys = func(input *appsync.ListApiKeysInput) (*appsync.ApiKey, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be made into its own top level function, so it can be used by testAccCheckAwsAppsyncApiKeyDestroy and testAccCheckAwsAppsyncApiKeyExists so they properly handle NextToken

var key appsync.ApiKey
for _, v := range resp.ApiKeys {
if *v.Id == *aws.String(Id) {
key = *v
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would recommend splitting this logic into two functions:

func testAccCheckAwsAppsyncApiKeyExists(resourceName string, apiKey *appsync.ApiKey)

which calls *apiKey = *v right here or otherwise returns an error that the key could not be found.

Then separately:

func testAccCheckAwsAppsyncApiKeyTillDate(apiKey *appsync.ApiKey, date time.Time)

which handles the date/time logic below.

@bflad bflad added the waiting-response Maintainers are waiting on response from community or contributor. label Apr 13, 2018
@bflad bflad added this to the v1.27.0 milestone Jul 9, 2018
@bflad
Copy link
Contributor

bflad commented Jul 9, 2018

Finished the implementation in a commit after the others. Passes acceptance testing:

make testacc TEST=./aws TESTARGS='-run=TestAccAWSAppsyncApiKey'
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./aws -v -run=TestAccAWSAppsyncApiKey -timeout 120m
=== RUN   TestAccAWSAppsyncApiKey_basic
--- PASS: TestAccAWSAppsyncApiKey_basic (12.30s)
=== RUN   TestAccAWSAppsyncApiKey_Description
--- PASS: TestAccAWSAppsyncApiKey_Description (18.26s)
=== RUN   TestAccAWSAppsyncApiKey_Expires
--- PASS: TestAccAWSAppsyncApiKey_Expires (19.80s)
PASS
ok  	github.com/terraform-providers/terraform-provider-aws/aws	50.401s

@bflad bflad merged commit 76d141d into hashicorp:master Jul 9, 2018
bflad added a commit that referenced this pull request Jul 9, 2018
@lucidprogrammer lucidprogrammer deleted the appsync_api_key branch July 10, 2018 00:15
@bflad
Copy link
Contributor

bflad commented Jul 11, 2018

This has been released in version 1.27.0 of the AWS provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading.

@ghost
Copy link

ghost commented Apr 4, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thanks!

@ghost ghost locked and limited conversation to collaborators Apr 4, 2020
@breathingdust breathingdust removed the waiting-response Maintainers are waiting on response from community or contributor. label Sep 17, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
new-resource Introduces a new resource. service/appsync Issues and PRs that pertain to the appsync service. size/L Managed by automation to categorize the size of a PR.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants