Skip to content

Commit

Permalink
Fix: Nil pointer on iam_service_api_key
Browse files Browse the repository at this point in the history
  • Loading branch information
kavya498 committed Feb 12, 2021
1 parent 21a5a83 commit dbadcec
Showing 1 changed file with 30 additions and 13 deletions.
43 changes: 30 additions & 13 deletions ibm/resource_ibm_iam_service_api_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func resourceIBMIAMServiceAPIkeyCreate(d *schema.ResourceData, meta interface{})
}

apiKey, response, err := iamIdentityClient.CreateAPIKey(createAPIKeyOptions)
if err != nil {
if err != nil || apiKey == nil {
return fmt.Errorf("[DEBUG] Service API Key creation Error: %s\n%s", err, response)
}

Expand Down Expand Up @@ -188,29 +188,46 @@ func resourceIBMIAMServiceAPIKeyRead(d *schema.ResourceData, meta interface{}) e
}

apiKey, response, err := iamIdentityClient.GetAPIKey(getAPIKeyOptions)
if err != nil {
if err != nil || apiKey == nil {
if response != nil && response.StatusCode == 404 {
d.SetId("")
return nil
}
return fmt.Errorf("[DEBUG] Error retrieving Service API Key: %s\n%s", err, response)
}

d.Set("name", *apiKey.Name)
d.Set("iam_service_id", *apiKey.IamID)
if apiKey.Name != nil {
d.Set("name", *apiKey.Name)
}
if apiKey.IamID != nil {
d.Set("iam_service_id", *apiKey.IamID)
}
if apiKey.Description != nil {
d.Set("description", *apiKey.Description)
}
d.Set("account_id", *apiKey.AccountID)
if apiKey.AccountID != nil {
d.Set("account_id", *apiKey.AccountID)
}
if *apiKey.Apikey != "" {
d.Set("apikey", *apiKey.Apikey)
}
d.Set("crn", *apiKey.CRN)
d.Set("entity_tag", *apiKey.EntityTag)
d.Set("locked", *apiKey.Locked)
d.Set("created_by", *apiKey.CreatedBy)
d.Set("created_at", apiKey.CreatedAt.String())
d.Set("modified_at", apiKey.ModifiedAt.String())
if apiKey.CRN != nil {
d.Set("crn", *apiKey.CRN)
}
if apiKey.EntityTag != nil {
d.Set("entity_tag", *apiKey.EntityTag)
}
if apiKey.Locked != nil {
d.Set("locked", *apiKey.Locked)
}
if apiKey.CreatedBy != nil {
d.Set("created_by", *apiKey.CreatedBy)
}
if apiKey.CreatedAt != nil {
d.Set("created_at", apiKey.CreatedAt.String())
}
if apiKey.ModifiedAt != nil {
d.Set("modified_at", apiKey.ModifiedAt.String())
}

return nil
}
Expand All @@ -228,7 +245,7 @@ func resourceIBMIAMServiceAPIKeyUpdate(d *schema.ResourceData, meta interface{})
}

apiKey, resp, err := iamIdentityClient.GetAPIKey(getAPIKeyOptions)
if err != nil {
if err != nil || apiKey == nil {
return fmt.Errorf("[DEBUG] Error retrieving Service API Key: %s\n%s", err, resp)
}

Expand Down

0 comments on commit dbadcec

Please sign in to comment.