-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
Add Lightsail load balancer resource #11405
Merged
ewbankkit
merged 20 commits into
hashicorp:main
from
akonrath:feature/lightsail-akonrath
Oct 20, 2022
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
26862f2
Added Lightsail load balancer resource, fixed 'disapear' typo in ligh…
akonrath 3e88cf8
Action Required: Removal of website/aws.erb File
brittandeyoung 4fa9805
Fixed: Action Required: Terraform Plugin SDK V2
brittandeyoung a4dcde1
Merge pull request #83 from brittandeyoung/patch-1
akonrath 43bbaf4
Removed conflicting changes
brittandeyoung 7f65c40
* Fixed usage of SetPartial
brittandeyoung 955b12f
Merge pull request #88 from brittandeyoung/patch-2
akonrath 9e4d71b
Fixed:
brittandeyoung b48d5c6
Merge branch 'main' of https://github.com/hashicorp/terraform-provide…
brittandeyoung 7c24c54
fixed: AWSR002
brittandeyoung 89b5467
Merge pull request #91 from brittandeyoung/patch-2
akonrath a2c4807
Created Internal Waiter files
brittandeyoung 6a146ba
FIxed:
brittandeyoung df2e0e3
added aws sdk import
brittandeyoung 26ae514
Fixed: Waiter input type
brittandeyoung d9beabd
Updated healthcheckpath test name
brittandeyoung be02e54
fixed import order an String format
brittandeyoung 72ad656
switched to aws.stringvalue
brittandeyoung cb72b94
resolved import order
brittandeyoung 9c4e385
Merge pull request #96 from brittandeyoung/patch-2
akonrath File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package waiter | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/lightsail" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
// LightsailOperationStatus is a method to check the status of a Lightsail Operation | ||
func LightsailOperationStatus(conn *lightsail.Lightsail, oid *string) resource.StateRefreshFunc { | ||
return func() (interface{}, string, error) { | ||
input := &lightsail.GetOperationInput{ | ||
OperationId: oid, | ||
} | ||
|
||
oidValue := aws.StringValue(oid) | ||
log.Printf("[DEBUG] Checking if Lightsail Operation (%s) is Completed", oidValue) | ||
|
||
output, err := conn.GetOperation(input) | ||
|
||
if err != nil { | ||
return output, "FAILED", err | ||
} | ||
|
||
if output.Operation == nil { | ||
return nil, "Failed", fmt.Errorf("Error retrieving Operation info for operation (%s)", oidValue) | ||
} | ||
|
||
log.Printf("[DEBUG] Lightsail Operation (%s) is currently %q", oidValue, *output.Operation.Status) | ||
return output, *output.Operation.Status, nil | ||
} | ||
} |
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,48 @@ | ||
package waiter | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go/service/lightsail" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
const ( | ||
// OperationStatusNotStarted is a OperationStatus enum value | ||
OperationStatusNotStarted = "NotStarted" | ||
// OperationStatusStarted is a OperationStatus enum value | ||
OperationStatusStarted = "Started" | ||
// OperationStatusFailed is a OperationStatus enum value | ||
OperationStatusFailed = "Failed" | ||
// OperationStatusCompleted is a OperationStatus enum value | ||
OperationStatusCompleted = "Completed" | ||
// OperationStatusSucceeded is a OperationStatus enum value | ||
OperationStatusSucceeded = "Succeeded" | ||
|
||
// OperationTimeout is the Timout Value for Operations | ||
OperationTimeout = 10 * time.Minute | ||
// OperationDelay is the Delay Value for Operations | ||
OperationDelay = 5 * time.Second | ||
// OperationMinTimeout is the MinTimout Value for Operations | ||
OperationMinTimeout = 3 * time.Second | ||
) | ||
|
||
// OperationCreated waits for an Operation to return Succeeded or Compleated | ||
func OperationCreated(conn *lightsail.Lightsail, oid *string) (*lightsail.GetOperationOutput, error) { | ||
stateConf := &resource.StateChangeConf{ | ||
Pending: []string{OperationStatusStarted}, | ||
Target: []string{OperationStatusCompleted, OperationStatusSucceeded}, | ||
Refresh: LightsailOperationStatus(conn, oid), | ||
Timeout: OperationTimeout, | ||
Delay: OperationDelay, | ||
MinTimeout: OperationMinTimeout, | ||
} | ||
|
||
outputRaw, err := stateConf.WaitForState() | ||
|
||
if output, ok := outputRaw.(*lightsail.GetOperationOutput); ok { | ||
return output, err | ||
} | ||
|
||
return nil, err | ||
} |
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,191 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"regexp" | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/awserr" | ||
"github.com/aws/aws-sdk-go/service/lightsail" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" | ||
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags" | ||
"github.com/terraform-providers/terraform-provider-aws/aws/internal/service/lightsail/waiter" | ||
) | ||
|
||
func resourceAwsLightsailLoadBalancer() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceAwsLightsailLoadBalancerCreate, | ||
Read: resourceAwsLightsailLoadBalancerRead, | ||
Update: resourceAwsLightsailLoadBalancerUpdate, | ||
Delete: resourceAwsLightsailLoadBalancerDelete, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validation.All( | ||
validation.StringLenBetween(2, 255), | ||
validation.StringMatch(regexp.MustCompile(`^[a-zA-Z]`), "must begin with an alphabetic character"), | ||
validation.StringMatch(regexp.MustCompile(`^[a-zA-Z0-9_\-.]+[^._\-]$`), "must contain only alphanumeric characters, underscores, hyphens, and dots"), | ||
), | ||
}, | ||
"health_check_path": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Default: "/", | ||
}, | ||
"instance_port": { | ||
Type: schema.TypeInt, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validation.IntBetween(0, 65535), | ||
}, | ||
"tags": tagsSchema(), | ||
"arn": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"created_at": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"dns_name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"protocol": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"public_ports": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Schema{Type: schema.TypeInt}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceAwsLightsailLoadBalancerCreate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).lightsailconn | ||
|
||
req := lightsail.CreateLoadBalancerInput{ | ||
HealthCheckPath: aws.String(d.Get("health_check_path").(string)), | ||
InstancePort: aws.Int64(int64(d.Get("instance_port").(int))), | ||
LoadBalancerName: aws.String(d.Get("name").(string)), | ||
} | ||
|
||
if v := d.Get("tags").(map[string]interface{}); len(v) > 0 { | ||
req.Tags = keyvaluetags.New(v).IgnoreAws().LightsailTags() | ||
} | ||
|
||
resp, err := conn.CreateLoadBalancer(&req) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if len(resp.Operations) == 0 { | ||
return fmt.Errorf("No operations found for CreateInstance request") | ||
} | ||
|
||
op := resp.Operations[0] | ||
d.SetId(d.Get("name").(string)) | ||
|
||
_, err = waiter.OperationCreated(conn, op.Id) | ||
if err != nil { | ||
return fmt.Errorf("Error waiting for load balancer (%s) to become ready: %s", d.Id(), err) | ||
} | ||
|
||
return resourceAwsLightsailLoadBalancerRead(d, meta) | ||
} | ||
|
||
func resourceAwsLightsailLoadBalancerRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).lightsailconn | ||
ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig | ||
|
||
resp, err := conn.GetLoadBalancer(&lightsail.GetLoadBalancerInput{ | ||
LoadBalancerName: aws.String(d.Id()), | ||
}) | ||
|
||
lb := resp.LoadBalancer | ||
|
||
if err != nil { | ||
if awsErr, ok := err.(awserr.Error); ok { | ||
if awsErr.Code() == "NotFoundException" { | ||
log.Printf("[WARN] Lightsail load balancer (%s) not found, removing from state", d.Id()) | ||
d.SetId("") | ||
return nil | ||
} | ||
return err | ||
} | ||
return err | ||
} | ||
|
||
d.Set("arn", lb.Arn) | ||
d.Set("created_at", lb.CreatedAt.Format(time.RFC3339)) | ||
d.Set("health_check_path", lb.HealthCheckPath) | ||
d.Set("instance_port", lb.InstancePort) | ||
d.Set("name", lb.Name) | ||
d.Set("protocol", lb.Protocol) | ||
d.Set("public_ports", lb.PublicPorts) | ||
d.Set("dns_name", lb.DnsName) | ||
|
||
if err := d.Set("tags", keyvaluetags.LightsailKeyValueTags(lb.Tags).IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map()); err != nil { | ||
return fmt.Errorf("error setting tags: %s", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceAwsLightsailLoadBalancerDelete(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).lightsailconn | ||
resp, err := conn.DeleteLoadBalancer(&lightsail.DeleteLoadBalancerInput{ | ||
LoadBalancerName: aws.String(d.Id()), | ||
}) | ||
|
||
op := resp.Operations[0] | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = waiter.OperationCreated(conn, op.Id) | ||
if err != nil { | ||
return fmt.Errorf("Error waiting for load balancer (%s) to become destroyed: %s", d.Id(), err) | ||
} | ||
|
||
return err | ||
} | ||
|
||
func resourceAwsLightsailLoadBalancerUpdate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).lightsailconn | ||
|
||
if d.HasChange("health_check_path") { | ||
_, err := conn.UpdateLoadBalancerAttribute(&lightsail.UpdateLoadBalancerAttributeInput{ | ||
AttributeName: aws.String("HealthCheckPath"), | ||
AttributeValue: aws.String(d.Get("health_check_path").(string)), | ||
LoadBalancerName: aws.String(d.Get("name").(string)), | ||
}) | ||
d.Set("health_check_path", d.Get("health_check_path").(string)) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
if d.HasChange("tags") { | ||
o, n := d.GetChange("tags") | ||
|
||
if err := keyvaluetags.LightsailUpdateTags(conn, d.Id(), o, n); err != nil { | ||
return fmt.Errorf("error updating Lightsail Instance (%s) tags: %s", d.Id(), err) | ||
} | ||
} | ||
|
||
return resourceAwsLightsailLoadBalancerRead(d, meta) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lb := resp.LoadBalancer
and reuse it for all others instead repeating it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the feedback, this change will be implemented once the following pull request is merged by @akonrath akonrath#96