Skip to content

Commit 06cbb77

Browse files
PriyaranimishraYour Name
authored andcommitted
cos satellite bucket creation (IBM-Cloud#3727)
* cos satellite bucket cos-satellite doesnot support firwall- update docs based on it added conflict for satellite location id added conflict for satellite location id cos-satellite does not support allowedip cos-satellite commit updated files updated based on comments commited updated based on the changes updated based on the changes * updated datasource * updated datasource Co-authored-by: Your Name <prani071@in.ibm.com>
1 parent 8a1b2eb commit 06cbb77

10 files changed

+1002
-120
lines changed

examples/ibm-cos-bucket/README.md

+41
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,46 @@ data "ibm_cos_bucket" "standard-ams03" {
193193

194194
* [Cloud Object Storage](https://github.com/IBM-Cloud/terraform-provider-ibm/tree/master/examples/ibm-cos-bucket)
195195

196+
<!-- COS SATELLITE PROJECT -->
197+
198+
## COS SATELLITE
199+
200+
The following example creates a bucket and add object versioning and expiration features on COS satellite location. As of now we are using existing cos instance to create bucket , so no need to create any cos instance via a terraform. We don't have any resource group in satellite.We can not use storage_class with Satellite location id.
201+
202+
* [IBM Satellite](https://cloud.ibm.com/docs/satellite?topic=satellite-getting-started)
203+
* [IBM COS Satellite](https://cloud.ibm.com/docs/cloud-object-storage?topic=cloud-object-storage-about-cos-satellite)
204+
205+
## Example Usage
206+
207+
```terraform
208+
data "ibm_resource_group" "group" {
209+
name = "Default"
210+
}
211+
212+
resource "ibm_satellite_location" "create_location" {
213+
location = var.location
214+
zones = var.location_zones
215+
managed_from = var.managed_from
216+
resource_group_id = data.ibm_resource_group.group.id
217+
}
218+
219+
resource "ibm_cos_bucket" "cos_bucket" {
220+
bucket_name = "cos-sat-terraform"
221+
resource_instance_id = data.ibm_resource_instance.cos_instance.id
222+
satellite_location_id = data.ibm_satellite_location.create_location.id
223+
object_versioning {
224+
enable = true
225+
}
226+
expire_rule {
227+
rule_id = "bucket-tf-rule1"
228+
enable = false
229+
days = 20
230+
prefix = "logs/"
231+
}
232+
}
233+
```
234+
235+
196236
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
197237

198238
## Requirements
@@ -213,6 +253,7 @@ data "ibm_cos_bucket" "standard-ams03" {
213253
|------|-------------|------|---------|
214254
| bucket_name | Name of the bucket. | `string` | yes |
215255
| resource_group_name | Name of the resource group. | `string` | yes |
256+
| satellite_location_id | satellite location. | `string` | no |
216257
| storage | The storage class that you want to use for the bucket. Supported values are **standard, vault, cold, flex, and smart**.| `string` | no |
217258
| region | The location for a cross-regional bucket. Supported values are **us, eu, and ap**. | `string` | no |
218259
| read_data_events | Enables sending log data to Activity Tracker and LogDNA to provide visibility into object read and write events. | `array` | no

examples/ibm-cos-bucket/main.tf

+16
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,19 @@ resource "ibm_cos_bucket_object" "base64" {
112112
content_base64 = "RW5jb2RlZCBpbiBiYXNlNjQ="
113113
key = "base64.txt"
114114
}
115+
116+
//Satellite Location
117+
resource "ibm_cos_bucket" "cos_bucket_sat" {
118+
bucket_name = var.bucket_name
119+
resource_instance_id = "crn:v1:bluemix:public:cloud-object-storage:satloc_wdc_c8jh7hfw0ppoapdqrmpg:a/d0c259a490e4488c83b62707ad3f5182:756ad6b6-72a6-4e55-8c94-b02e51e708b3::"
120+
satellite_location_id = var.satellite_location_id
121+
object_versioning {
122+
enable = true
123+
}
124+
expire_rule {
125+
rule_id = "bucket-tf-rule1"
126+
enable = false
127+
days = 20
128+
prefix = "logs/"
129+
}
130+
}

examples/ibm-cos-bucket/provider.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ provider "ibm" {
1414
iaas_classic_username = var.iaas_classic_username
1515
iaas_classic_api_key = var.iaas_classic_api_key
1616
ibmcloud_api_key = var.ibmcloud_api_key
17-
}
17+
}

examples/ibm-cos-bucket/variables.tf

+6-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ variable "expire_days" {
4343
}
4444

4545
variable "expire_date" {
46-
default = ""
46+
default = "2022-06-09"
4747
}
4848

4949
variable "expire_prefix" {
@@ -88,4 +88,8 @@ variable "maximum_retention" {
8888

8989
variable "quota" {
9090
default = "1"
91-
}
91+
}
92+
93+
variable "satellite_location_id" {
94+
default = ""
95+
}

ibm/acctest/acctest.go

+14
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,10 @@ var Scc_posture_collector_id_scope_update []string
167167
//ROKS Cluster
168168
var ClusterName string
169169

170+
// Satellite instance
171+
var Satellite_location_id string
172+
var Satellite_Resource_instance_id string
173+
170174
//Dedicated host
171175
var HostPoolID string
172176

@@ -898,6 +902,16 @@ func init() {
898902
fmt.Println("[INFO] Set the environment variable IBM_CONTAINER_CLUSTER_NAME for ibm_container_nlb_dns resource or datasource else tests will fail if this is not set correctly")
899903
}
900904

905+
Satellite_location_id = os.Getenv("SATELLITE_LOCATION_ID")
906+
if Satellite_location_id == "" {
907+
fmt.Println("[INFO] Set the environment variable SATELLITE_LOCATION_ID for ibm_cos_bucket satellite location resource or datasource else tests will fail if this is not set correctly")
908+
}
909+
910+
Satellite_Resource_instance_id = os.Getenv("SATELLITE_RESOURCE_INSTANCE_ID")
911+
if Satellite_Resource_instance_id == "" {
912+
fmt.Println("[INFO] Set the environment variable SATELLITE_RESOURCE_INSTANCE_ID for ibm_cos_bucket satellite location resource or datasource else tests will fail if this is not set correctly")
913+
}
914+
901915
HostPoolID = os.Getenv("IBM_CONTAINER_DEDICATEDHOST_POOL_ID")
902916
if HostPoolID == "" {
903917
fmt.Println("[INFO] Set the environment variable IBM_CONTAINER_DEDICATEDHOST_POOL_ID for ibm_container_vpc_cluster resource to test dedicated host functionality")

ibm/service/cos/data_source_ibm_cos_bucket.go

+80-41
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ package cos
55

66
import (
77
"fmt"
8-
"strings"
9-
"time"
10-
118
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
129
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
1310
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/validate"
@@ -18,6 +15,8 @@ import (
1815
"github.com/IBM/ibm-cos-sdk-go/aws/session"
1916
"github.com/IBM/ibm-cos-sdk-go/service/s3"
2017
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
18+
"strings"
19+
"time"
2120
)
2221

2322
var bucketTypes = []string{"single_site_location", "region_location", "cross_region_location"}
@@ -32,24 +31,35 @@ func DataSourceIBMCosBucket() *schema.Resource {
3231
Required: true,
3332
},
3433
"bucket_type": {
35-
Type: schema.TypeString,
36-
ValidateFunc: validate.ValidateAllowedStringValues(bucketTypes),
37-
Required: true,
34+
Type: schema.TypeString,
35+
ValidateFunc: validate.ValidateAllowedStringValues(bucketTypes),
36+
Optional: true,
37+
RequiredWith: []string{"bucket_region"},
38+
ConflictsWith: []string{"satellite_location_id"},
3839
},
3940
"bucket_region": {
40-
Type: schema.TypeString,
41-
Required: true,
41+
Type: schema.TypeString,
42+
Optional: true,
43+
RequiredWith: []string{"bucket_type"},
44+
ConflictsWith: []string{"satellite_location_id"},
4245
},
4346
"resource_instance_id": {
4447
Type: schema.TypeString,
4548
Required: true,
4649
},
50+
"satellite_location_id": {
51+
Type: schema.TypeString,
52+
Optional: true,
53+
ConflictsWith: []string{"bucket_type", "bucket_region"},
54+
ExactlyOneOf: []string{"satellite_location_id", "bucket_region"},
55+
},
4756
"endpoint_type": {
48-
Type: schema.TypeString,
49-
Optional: true,
50-
ValidateFunc: validate.ValidateAllowedStringValues([]string{"public", "private", "direct"}),
51-
Description: "public or private",
52-
Default: "public",
57+
Type: schema.TypeString,
58+
Optional: true,
59+
ValidateFunc: validate.ValidateAllowedStringValues([]string{"public", "private", "direct"}),
60+
Description: "public or private",
61+
ConflictsWith: []string{"satellite_location_id"},
62+
Default: "public",
5363
},
5464
"crn": {
5565
Type: schema.TypeString,
@@ -327,14 +337,32 @@ func dataSourceIBMCosBucketRead(d *schema.ResourceData, meta interface{}) error
327337
serviceID := d.Get("resource_instance_id").(string)
328338
bucketType := d.Get("bucket_type").(string)
329339
bucketRegion := d.Get("bucket_region").(string)
330-
var endpointType = d.Get("endpoint_type").(string)
331-
apiEndpoint, apiEndpointPrivate, directApiEndpoint := SelectCosApi(bucketLocationConvert(bucketType), bucketRegion)
332-
if endpointType == "private" {
333-
apiEndpoint = apiEndpointPrivate
340+
endpointType := d.Get("endpoint_type").(string)
341+
342+
var satlc_id, apiEndpoint, apiEndpointPrivate, directApiEndpoint string
343+
344+
if satlc, ok := d.GetOk("satellite_location_id"); ok {
345+
satlc_id = satlc.(string)
346+
satloc_guid := strings.Split(serviceID, ":")
347+
bucketsatcrn := satloc_guid[7]
348+
serviceID = bucketsatcrn
349+
bucketType = "sl"
334350
}
335-
if endpointType == "direct" {
336-
apiEndpoint = directApiEndpoint
351+
352+
if bucketType == "sl" {
353+
apiEndpoint = SelectSatlocCosApi(bucketType, serviceID, satlc_id)
354+
355+
} else {
356+
apiEndpoint, apiEndpointPrivate, directApiEndpoint = SelectCosApi(bucketLocationConvert(bucketType), bucketRegion)
357+
if endpointType == "private" {
358+
apiEndpoint = apiEndpointPrivate
359+
}
360+
if endpointType == "direct" {
361+
apiEndpoint = directApiEndpoint
362+
}
363+
337364
}
365+
338366
apiEndpoint = conns.EnvFallBack([]string{"IBMCLOUD_COS_ENDPOINT"}, apiEndpoint)
339367
if apiEndpoint == "" {
340368
return fmt.Errorf("[ERROR] The endpoint doesn't exists for given location %s and endpoint type %s", bucketRegion, endpointType)
@@ -372,26 +400,31 @@ func dataSourceIBMCosBucketRead(d *schema.ResourceData, meta interface{}) error
372400
return fmt.Errorf("failed waiting for bucket %s to be created, %v",
373401
bucketName, err)
374402
}
375-
bucketLocationInput := &s3.GetBucketLocationInput{
376-
Bucket: aws.String(bucketName),
377-
}
378-
bucketLocationConstraint, err := s3Client.GetBucketLocation(bucketLocationInput)
379-
if err != nil {
380-
return err
381-
}
382-
bLocationConstraint := *bucketLocationConstraint.LocationConstraint
383403

384-
if singleSiteLocationRegex.MatchString(bLocationConstraint) {
385-
d.Set("single_site_location", strings.Split(bLocationConstraint, "-")[0])
386-
d.Set("storage_class", strings.Split(bLocationConstraint, "-")[1])
387-
}
388-
if regionLocationRegex.MatchString(bLocationConstraint) {
389-
d.Set("region_location", fmt.Sprintf("%s-%s", strings.Split(bLocationConstraint, "-")[0], strings.Split(bLocationConstraint, "-")[1]))
390-
d.Set("storage_class", strings.Split(bLocationConstraint, "-")[2])
391-
}
392-
if crossRegionLocationRegex.MatchString(bLocationConstraint) {
393-
d.Set("cross_region_location", strings.Split(bLocationConstraint, "-")[0])
394-
d.Set("storage_class", strings.Split(bLocationConstraint, "-")[1])
404+
if bucketType != "sl" {
405+
bucketLocationInput := &s3.GetBucketLocationInput{
406+
Bucket: aws.String(bucketName),
407+
}
408+
bucketLocationConstraint, err := s3Client.GetBucketLocation(bucketLocationInput)
409+
if err != nil {
410+
return err
411+
}
412+
bLocationConstraint := *bucketLocationConstraint.LocationConstraint
413+
414+
if singleSiteLocationRegex.MatchString(bLocationConstraint) {
415+
d.Set("single_site_location", strings.Split(bLocationConstraint, "-")[0])
416+
d.Set("storage_class", strings.Split(bLocationConstraint, "-")[1])
417+
}
418+
if regionLocationRegex.MatchString(bLocationConstraint) {
419+
d.Set("region_location", fmt.Sprintf("%s-%s", strings.Split(bLocationConstraint, "-")[0], strings.Split(bLocationConstraint, "-")[1]))
420+
d.Set("storage_class", strings.Split(bLocationConstraint, "-")[2])
421+
}
422+
if crossRegionLocationRegex.MatchString(bLocationConstraint) {
423+
d.Set("cross_region_location", strings.Split(bLocationConstraint, "-")[0])
424+
d.Set("storage_class", strings.Split(bLocationConstraint, "-")[1])
425+
}
426+
} else {
427+
d.Set("satellite_location_id", satlc_id)
395428
}
396429

397430
head, err := s3Client.HeadBucket(headInput)
@@ -420,14 +453,20 @@ func dataSourceIBMCosBucketRead(d *schema.ResourceData, meta interface{}) error
420453
if endpointType == "private" {
421454
sess.SetServiceURL("https://config.private.cloud-object-storage.cloud.ibm.com/v1")
422455
}
423-
bucketPtr, response, err := sess.GetBucketConfig(getBucketConfigOptions)
424456

457+
if bucketType == "sl" {
458+
satconfig := fmt.Sprintf("https://config.%s.%s.cloud-object-storage.appdomain.cloud/v1", serviceID, satlc_id)
459+
460+
sess.SetServiceURL(satconfig)
461+
462+
}
463+
464+
bucketPtr, response, err := sess.GetBucketConfig(getBucketConfigOptions)
425465
if err != nil {
426466
return fmt.Errorf("[ERROR] Error in getting bucket info rule: %s\n%s", err, response)
427467
}
428468

429469
if bucketPtr != nil {
430-
431470
if bucketPtr.Firewall != nil {
432471
d.Set("allowed_ip", flex.FlattenStringList(bucketPtr.Firewall.AllowedIp))
433472
}
@@ -520,7 +559,7 @@ func bucketLocationConvert(locationtype string) string {
520559
return "rl"
521560
}
522561
if locationtype == "single_site_location" {
523-
return "crl"
562+
return "ssl"
524563
}
525564
return ""
526565
}

0 commit comments

Comments
 (0)