<commit_before>package main

import (
	alks "github.com/Cox-Automotive/alks-go"
	"github.com/hashicorp/terraform/helper/schema"
	"log"
)

func resourceAlksLtk() *schema.Resource {
	return &schema.Resource{
		Create: resourceAlksLtkCreate,
		Read:   resourceAlksLtkRead,
		Delete: resourceAlksLtkDelete,

		SchemaVersion: 1,

		Schema: map[string]*schema.Schema{
			"iam_username": &schema.Schema{
				Type:     schema.TypeString,
				Required: true,
				ForceNew: true,
			},
			"iam_user_arn": &schema.Schema{
				Type:     schema.TypeString,
				Computed: true,
			},
			"access_key": &schema.Schema{
				Sensitive: true,
				Type:      schema.TypeString,
				Computed:  true,
			},
			"secret_key": &schema.Schema{
				Sensitive: true,
				Type:      schema.TypeString,
				Computed:  true,
			},
		},
	}
}

func resourceAlksLtkCreate(d *schema.ResourceData, meta interface{}) error {
	log.Printf("[INFO] ALKS LTK User Create")

	var iamUsername = d.Get("iam_username").(string)

	client := meta.(*alks.Client)
	resp, err := client.CreateLongTermKey(iamUsername)

	if err != nil {
		return err
	}

	d.SetId(iamUsername)
	_ = d.Set("iam_user_arn", resp.IAMUserArn)
	_ = d.Set("access_key", resp.AccessKey)
	_ = d.Set("secret_key", resp.SecretKey)

	log.Printf("[INFO] alks_ltk.id: %v", d.Id())

	return nil
}

func resourceAlksLtkRead(d *schema.ResourceData, meta interface{}) error {
	log.Printf("[INFO] ALKS LTK Users Read")

	client := meta.(*alks.Client)
	resp, err := client.GetLongTermKey(d.Id())

	if err != nil {
		d.SetId("")
		return nil
	}

	log.Printf("[INFO] alks_ltk.id: %v", d.Id())

	return populateResourceDataFromLTK(resp, d)
}

func resourceAlksLtkDelete(d *schema.ResourceData, meta interface{}) error {
	log.Printf("[INFO] ALKS LTK User Delete")

	client := meta.(*alks.Client)
	_, err := client.DeleteLongTermKey(d.Id())

	if err != nil {
		return err
	}

	return nil
}

func populateResourceDataFromLTK(longTermKey *alks.GetLongTermKeyResponse, d *schema.ResourceData) error {
	d.SetId(longTermKey.UserName)
	_ = d.Set("access_key", longTermKey.AccessKeyID)
	_ = d.Set("create_date", longTermKey.CreateDate)

	return nil
}<commit_msg>Added exists and import functionality to the resource provider.<commit_after>package main

import (
	alks "github.com/Cox-Automotive/alks-go"
	"github.com/hashicorp/terraform/helper/schema"
	"log"
)

func resourceAlksLtk() *schema.Resource {
	return &schema.Resource{
		Create: resourceAlksLtkCreate,
		Read:   resourceAlksLtkRead,
		Delete: resourceAlksLtkDelete,
		Exists: resourceAlksLtkExists,
		Importer: &schema.ResourceImporter{
			// Terraform provided importer
			State: schema.ImportStatePassthrough,
		},

		SchemaVersion: 1,

		Schema: map[string]*schema.Schema{
			"iam_username": &schema.Schema{
				Type:     schema.TypeString,
				Required: true,
				ForceNew: true,
			},
			"iam_user_arn": &schema.Schema{
				Type:     schema.TypeString,
				Computed: true,
			},
			"access_key": &schema.Schema{
				Sensitive: true,
				Type:      schema.TypeString,
				Computed:  true,
			},
			"secret_key": &schema.Schema{
				Sensitive: true,
				Type:      schema.TypeString,
				Computed:  true,
			},
		},
	}
}

func resourceAlksLtkCreate(d *schema.ResourceData, meta interface{}) error {
	log.Printf("[INFO] ALKS LTK User Create")

	var iamUsername = d.Get("iam_username").(string)

	client := meta.(*alks.Client)
	resp, err := client.CreateLongTermKey(iamUsername)

	if err != nil {
		return err
	}

	d.SetId(iamUsername)
	_ = d.Set("iam_user_arn", resp.IAMUserArn)
	_ = d.Set("access_key", resp.AccessKey)
	_ = d.Set("secret_key", resp.SecretKey)

	log.Printf("[INFO] alks_ltk.id: %v", d.Id())

	return resourceAlksLtkRead(d, meta)
}

func resourceAlksLtkRead(d *schema.ResourceData, meta interface{}) error {
	log.Printf("[INFO] ALKS LTK User Read")

	client := meta.(*alks.Client)
	resp, err := client.GetLongTermKey(d.Id())

	if err != nil {
		d.SetId("")
		return nil
	}

	log.Printf("[INFO] alks_ltk.id: %v", d.Id())

	return populateResourceDataFromLTK(resp, d)
}

func resourceAlksLtkDelete(d *schema.ResourceData, meta interface{}) error {
	log.Printf("[INFO] ALKS LTK User Delete")

	client := meta.(*alks.Client)
	_, err := client.DeleteLongTermKey(d.Id())

	if err != nil {
		return err
	}

	return nil
}

func resourceAlksLtkExists(d *schema.ResourceData, meta interface{}) (bool, error) {
	log.Printf("[INFO] ALKS LTK User Exists")

	client := meta.(*alks.Client)
	resp, err := client.GetLongTermKey(d.Id())

	if err != nil {
		return false, err
	}

	// We can get a 200, but an empty string so this is the condition to check for.
	if len(resp.LongTermKey.UserName) == 0 {
		return false, nil
	}

	return true, nil
}

func populateResourceDataFromLTK(longTermKey *alks.GetLongTermKeyResponse, d *schema.ResourceData) error {
	d.SetId(longTermKey.UserName)
	_ = d.Set("access_key", longTermKey.AccessKeyID)
	_ = d.Set("create_date", longTermKey.CreateDate)

	return nil
}
{'fraction_non_alphanumeric': 0.13980229977809158, 'fraction_numerical': 0.0012104095218882388, 'mean_word_length': 4.074718526100307, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5246306', 'n_tokens_mistral': 1971, 'n_tokens_neox': 1806, 'n_words': 373}