|
| 1 | +package redshiftserverless |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + |
| 7 | + "github.com/aws/aws-sdk-go/aws" |
| 8 | + "github.com/aws/aws-sdk-go/service/redshiftserverless" |
| 9 | + "github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" |
| 12 | + "github.com/hashicorp/terraform-provider-aws/internal/conns" |
| 13 | + "github.com/hashicorp/terraform-provider-aws/internal/flex" |
| 14 | + "github.com/hashicorp/terraform-provider-aws/internal/tfresource" |
| 15 | +) |
| 16 | + |
| 17 | +func ResourceEndpointAccess() *schema.Resource { |
| 18 | + return &schema.Resource{ |
| 19 | + Create: resourceEndpointAccessCreate, |
| 20 | + Read: resourceEndpointAccessRead, |
| 21 | + Update: resourceEndpointAccessUpdate, |
| 22 | + Delete: resourceEndpointAccessDelete, |
| 23 | + |
| 24 | + Importer: &schema.ResourceImporter{ |
| 25 | + State: schema.ImportStatePassthrough, |
| 26 | + }, |
| 27 | + |
| 28 | + Schema: map[string]*schema.Schema{ |
| 29 | + "arn": { |
| 30 | + Type: schema.TypeString, |
| 31 | + Computed: true, |
| 32 | + }, |
| 33 | + "address": { |
| 34 | + Type: schema.TypeString, |
| 35 | + Computed: true, |
| 36 | + }, |
| 37 | + "port": { |
| 38 | + Type: schema.TypeInt, |
| 39 | + Computed: true, |
| 40 | + }, |
| 41 | + "vpc_endpoint": { |
| 42 | + Type: schema.TypeList, |
| 43 | + Computed: true, |
| 44 | + Elem: &schema.Resource{ |
| 45 | + Schema: map[string]*schema.Schema{ |
| 46 | + "vpc_endpoint_id": { |
| 47 | + Type: schema.TypeString, |
| 48 | + Computed: true, |
| 49 | + }, |
| 50 | + "vpc_id": { |
| 51 | + Type: schema.TypeString, |
| 52 | + Computed: true, |
| 53 | + }, |
| 54 | + "network_interface": { |
| 55 | + Type: schema.TypeList, |
| 56 | + Computed: true, |
| 57 | + Elem: &schema.Resource{ |
| 58 | + Schema: map[string]*schema.Schema{ |
| 59 | + "availability_zone": { |
| 60 | + Type: schema.TypeString, |
| 61 | + Computed: true, |
| 62 | + }, |
| 63 | + "network_interface_id": { |
| 64 | + Type: schema.TypeString, |
| 65 | + Computed: true, |
| 66 | + }, |
| 67 | + "private_ip_address": { |
| 68 | + Type: schema.TypeString, |
| 69 | + Computed: true, |
| 70 | + }, |
| 71 | + "subnet_id": { |
| 72 | + Type: schema.TypeString, |
| 73 | + Computed: true, |
| 74 | + }, |
| 75 | + }, |
| 76 | + }, |
| 77 | + }, |
| 78 | + }, |
| 79 | + }, |
| 80 | + }, |
| 81 | + "workgroup_name": { |
| 82 | + Type: schema.TypeString, |
| 83 | + Required: true, |
| 84 | + ForceNew: true, |
| 85 | + }, |
| 86 | + "vpc_security_group_ids": { |
| 87 | + Type: schema.TypeSet, |
| 88 | + Optional: true, |
| 89 | + Computed: true, |
| 90 | + Elem: &schema.Schema{ |
| 91 | + Type: schema.TypeString, |
| 92 | + }, |
| 93 | + }, |
| 94 | + "subnet_ids": { |
| 95 | + Type: schema.TypeSet, |
| 96 | + Required: true, |
| 97 | + ForceNew: true, |
| 98 | + Elem: &schema.Schema{ |
| 99 | + Type: schema.TypeString, |
| 100 | + }, |
| 101 | + }, |
| 102 | + "endpoint_name": { |
| 103 | + Type: schema.TypeString, |
| 104 | + Required: true, |
| 105 | + ForceNew: true, |
| 106 | + ValidateFunc: validation.StringLenBetween(1, 30), |
| 107 | + }, |
| 108 | + }, |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +func resourceEndpointAccessCreate(d *schema.ResourceData, meta interface{}) error { |
| 113 | + conn := meta.(*conns.AWSClient).RedshiftServerlessConn |
| 114 | + |
| 115 | + input := redshiftserverless.CreateEndpointAccessInput{ |
| 116 | + WorkgroupName: aws.String(d.Get("workgroup_name").(string)), |
| 117 | + EndpointName: aws.String(d.Get("endpoint_name").(string)), |
| 118 | + } |
| 119 | + |
| 120 | + if v, ok := d.GetOk("vpc_security_group_ids"); ok && v.(*schema.Set).Len() > 0 { |
| 121 | + input.VpcSecurityGroupIds = flex.ExpandStringSet(v.(*schema.Set)) |
| 122 | + } |
| 123 | + |
| 124 | + if v, ok := d.GetOk("subnet_ids"); ok && v.(*schema.Set).Len() > 0 { |
| 125 | + input.SubnetIds = flex.ExpandStringSet(v.(*schema.Set)) |
| 126 | + } |
| 127 | + |
| 128 | + out, err := conn.CreateEndpointAccess(&input) |
| 129 | + |
| 130 | + if err != nil { |
| 131 | + return fmt.Errorf("error creating Redshift Serverless Endpoint Access: %w", err) |
| 132 | + } |
| 133 | + |
| 134 | + d.SetId(aws.StringValue(out.Endpoint.EndpointName)) |
| 135 | + |
| 136 | + if _, err := waitEndpointAccessActive(conn, d.Id()); err != nil { |
| 137 | + return fmt.Errorf("error waiting for Redshift Serverless Endpoint Access (%s) to be created: %w", d.Id(), err) |
| 138 | + } |
| 139 | + |
| 140 | + return resourceEndpointAccessRead(d, meta) |
| 141 | +} |
| 142 | + |
| 143 | +func resourceEndpointAccessRead(d *schema.ResourceData, meta interface{}) error { |
| 144 | + conn := meta.(*conns.AWSClient).RedshiftServerlessConn |
| 145 | + |
| 146 | + out, err := FindEndpointAccessByName(conn, d.Id()) |
| 147 | + if !d.IsNewResource() && tfresource.NotFound(err) { |
| 148 | + log.Printf("[WARN] Redshift Serverless EndpointAccess (%s) not found, removing from state", d.Id()) |
| 149 | + d.SetId("") |
| 150 | + return nil |
| 151 | + } |
| 152 | + |
| 153 | + if err != nil { |
| 154 | + return fmt.Errorf("error reading Redshift Serverless Endpoint Access (%s): %w", d.Id(), err) |
| 155 | + } |
| 156 | + |
| 157 | + d.Set("address", out.Address) |
| 158 | + d.Set("port", out.Port) |
| 159 | + d.Set("arn", out.EndpointArn) |
| 160 | + d.Set("endpoint_name", out.EndpointName) |
| 161 | + d.Set("workgroup_name", out.WorkgroupName) |
| 162 | + d.Set("subnet_ids", flex.FlattenStringSet(out.SubnetIds)) |
| 163 | + |
| 164 | + result := make([]*string, 0, len(out.VpcSecurityGroups)) |
| 165 | + |
| 166 | + for _, v := range out.VpcSecurityGroups { |
| 167 | + result = append(result, v.VpcSecurityGroupId) |
| 168 | + } |
| 169 | + d.Set("vpc_security_group_ids", flex.FlattenStringSet(result)) |
| 170 | + |
| 171 | + if err := d.Set("vpc_endpoint", []interface{}{flattenVPCEndpoint(out.VpcEndpoint)}); err != nil { |
| 172 | + return fmt.Errorf("setting vpc_endpoint: %w", err) |
| 173 | + } |
| 174 | + |
| 175 | + return nil |
| 176 | +} |
| 177 | + |
| 178 | +func resourceEndpointAccessUpdate(d *schema.ResourceData, meta interface{}) error { |
| 179 | + conn := meta.(*conns.AWSClient).RedshiftServerlessConn |
| 180 | + |
| 181 | + input := &redshiftserverless.UpdateEndpointAccessInput{ |
| 182 | + EndpointName: aws.String(d.Id()), |
| 183 | + } |
| 184 | + |
| 185 | + if v, ok := d.GetOk("vpc_security_group_ids"); ok && v.(*schema.Set).Len() > 0 { |
| 186 | + input.VpcSecurityGroupIds = flex.ExpandStringSet(v.(*schema.Set)) |
| 187 | + } |
| 188 | + |
| 189 | + _, err := conn.UpdateEndpointAccess(input) |
| 190 | + if err != nil { |
| 191 | + return fmt.Errorf("error updating Redshift Serverless Endpoint Access (%s): %w", d.Id(), err) |
| 192 | + } |
| 193 | + |
| 194 | + if _, err := waitEndpointAccessActive(conn, d.Id()); err != nil { |
| 195 | + return fmt.Errorf("error waiting for Redshift Serverless Endpoint Access (%s) to be updated: %w", d.Id(), err) |
| 196 | + } |
| 197 | + |
| 198 | + return resourceEndpointAccessRead(d, meta) |
| 199 | +} |
| 200 | + |
| 201 | +func resourceEndpointAccessDelete(d *schema.ResourceData, meta interface{}) error { |
| 202 | + conn := meta.(*conns.AWSClient).RedshiftServerlessConn |
| 203 | + |
| 204 | + deleteInput := redshiftserverless.DeleteEndpointAccessInput{ |
| 205 | + EndpointName: aws.String(d.Id()), |
| 206 | + } |
| 207 | + |
| 208 | + log.Printf("[DEBUG] Deleting Redshift Serverless EndpointAccess: %s", d.Id()) |
| 209 | + _, err := conn.DeleteEndpointAccess(&deleteInput) |
| 210 | + |
| 211 | + if err != nil { |
| 212 | + if tfawserr.ErrCodeEquals(err, redshiftserverless.ErrCodeResourceNotFoundException) { |
| 213 | + return nil |
| 214 | + } |
| 215 | + return err |
| 216 | + } |
| 217 | + |
| 218 | + if _, err := waitEndpointAccessDeleted(conn, d.Id()); err != nil { |
| 219 | + return fmt.Errorf("error waiting for Redshift Serverless Endpoint Access (%s) delete: %w", d.Id(), err) |
| 220 | + } |
| 221 | + |
| 222 | + return nil |
| 223 | +} |
0 commit comments