Skip to content

Commit

Permalink
Fix test bucket endpoints (IBM-Cloud#5695)
Browse files Browse the repository at this point in the history
* add changes for test bucket endpoints

* update code

* fix for test cloud account url

* fix endpoints in datasource

* add FileFallBack check to IBMCOSBucketExists

* remove hardcoded test cloud url

* update code

---------

Co-authored-by: Deeksha Sharma <deekshasharma@Deekshas-MacBook-Pro.local>
  • Loading branch information
IBM-Deeksha and Deeksha Sharma authored Oct 16, 2024
1 parent 839fe1d commit 446909f
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 32 deletions.
18 changes: 12 additions & 6 deletions ibm/service/cos/data_source_ibm_cos_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ func dataSourceIBMCosBucketRead(d *schema.ResourceData, meta interface{}) error
keyProtectFlag = true
}

var satlc_id, apiEndpoint, apiEndpointPrivate, directApiEndpoint, visibility string
var satlc_id, apiEndpoint, apiEndpointPublic, apiEndpointPrivate, directApiEndpoint, visibility string

if satlc, ok := d.GetOk("satellite_location_id"); ok {
satlc_id = satlc.(string)
Expand All @@ -714,7 +714,7 @@ func dataSourceIBMCosBucketRead(d *schema.ResourceData, meta interface{}) error
apiEndpoint = SelectSatlocCosApi(bucketType, serviceID, satlc_id)

} else {
apiEndpoint, apiEndpointPrivate, directApiEndpoint = SelectCosApi(bucketLocationConvert(bucketType), bucketRegion, false)
apiEndpoint, apiEndpointPrivate, directApiEndpoint = SelectCosApi(bucketLocationConvert(bucketType), bucketRegion)
visibility = endpointType
if endpointType == "private" {
apiEndpoint = apiEndpointPrivate
Expand Down Expand Up @@ -811,10 +811,16 @@ func dataSourceIBMCosBucketRead(d *schema.ResourceData, meta interface{}) error
bucketCRN := fmt.Sprintf("%s:%s:%s", strings.Replace(serviceID, "::", "", -1), "bucket", bucketName)
d.Set("crn", bucketCRN)
d.Set("resource_instance_id", serviceID)
apiEndpoint, apiEndpointPrivate, directApiEndpoint = SelectCosApi(bucketLocationConvert(bucketType), bucketRegion, strings.Contains(apiEndpoint, "test"))
d.Set("s3_endpoint_public", apiEndpoint)
d.Set("s3_endpoint_private", apiEndpointPrivate)
d.Set("s3_endpoint_direct", directApiEndpoint)

testEnv := strings.Contains(apiEndpoint, ".test.")
apiEndpointPublic, apiEndpointPrivate, directApiEndpoint = SelectCosApi(bucketLocationConvert(bucketType), bucketRegion)
if testEnv {
d.Set(fmt.Sprintf("s3_endpoint_%s", endpointType), apiEndpoint)
} else {
d.Set("s3_endpoint_public", apiEndpointPublic)
d.Set("s3_endpoint_private", apiEndpointPrivate)
d.Set("s3_endpoint_direct", directApiEndpoint)
}
sess, err := meta.(conns.ClientSession).CosConfigV1API()
if err != nil {
return err
Expand Down
37 changes: 22 additions & 15 deletions ibm/service/cos/resource_ibm_cos_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@ func resourceIBMCOSBucketUpdate(d *schema.ResourceData, meta interface{}) error
if apiType == "sl" {
apiEndpoint = SelectSatlocCosApi(apiType, serviceID, bLocation)
} else {
apiEndpoint, apiEndpointPrivate, directApiEndpoint = SelectCosApi(apiType, bLocation, false)
apiEndpoint, apiEndpointPrivate, directApiEndpoint = SelectCosApi(apiType, bLocation)
visibility = endpointType
if endpointType == "private" {
apiEndpoint = apiEndpointPrivate
Expand Down Expand Up @@ -1129,7 +1129,7 @@ func resourceIBMCOSBucketRead(d *schema.ResourceData, meta interface{}) error {
if apiType == "sl" {
apiEndpoint = SelectSatlocCosApi(apiType, serviceID, bLocation)
} else {
apiEndpointPublic, apiEndpointPrivate, directApiEndpoint = SelectCosApi(apiType, bLocation, false)
apiEndpointPublic, apiEndpointPrivate, directApiEndpoint = SelectCosApi(apiType, bLocation)
apiEndpoint = apiEndpointPublic
if endpointType == "private" {
apiEndpoint = apiEndpointPrivate
Expand Down Expand Up @@ -1218,10 +1218,16 @@ func resourceIBMCOSBucketRead(d *schema.ResourceData, meta interface{}) error {
d.Set("resource_instance_id", serviceID)
d.Set("bucket_name", bucketName)

apiEndpointPublic, apiEndpointPrivate, directApiEndpoint = SelectCosApi(apiType, bLocation, strings.Contains(apiEndpoint, "test"))
d.Set("s3_endpoint_public", apiEndpointPublic)
d.Set("s3_endpoint_private", apiEndpointPrivate)
d.Set("s3_endpoint_direct", directApiEndpoint)
testEnv := strings.Contains(apiEndpoint, ".test.")
apiEndpointPublic, apiEndpointPrivate, directApiEndpoint = SelectCosApi(apiType, bLocation)

if testEnv {
d.Set(fmt.Sprintf("s3_endpoint_%s", endpointType), apiEndpoint)
} else {
d.Set("s3_endpoint_public", apiEndpointPublic)
d.Set("s3_endpoint_private", apiEndpointPrivate)
d.Set("s3_endpoint_direct", directApiEndpoint)
}
if endpointType != "" {
d.Set("endpoint_type", endpointType)
}
Expand All @@ -1241,7 +1247,6 @@ func resourceIBMCOSBucketRead(d *schema.ResourceData, meta interface{}) error {
if apiType == "sl" {

satconfig := fmt.Sprintf("https://config.%s.%s.cloud-object-storage.appdomain.cloud/v1", serviceID, bLocation)

sess.SetServiceURL(satconfig)
}

Expand Down Expand Up @@ -1427,7 +1432,7 @@ func resourceIBMCOSBucketCreate(d *schema.ResourceData, meta interface{}) error
apiEndpoint = SelectSatlocCosApi(apiType, serviceID, bLocation)

} else {
apiEndpoint, privateApiEndpoint, directApiEndpoint = SelectCosApi(apiType, bLocation, false)
apiEndpoint, privateApiEndpoint, directApiEndpoint = SelectCosApi(apiType, bLocation)
visibility = endpointType
if endpointType == "private" {
apiEndpoint = privateApiEndpoint
Expand Down Expand Up @@ -1552,7 +1557,7 @@ func resourceIBMCOSBucketDelete(d *schema.ResourceData, meta interface{}) error
apiEndpoint = SelectSatlocCosApi(apiType, serviceID, bLocation)

} else {
apiEndpoint, apiEndpointPrivate, directApiEndpoint = SelectCosApi(apiType, bLocation, false)
apiEndpoint, apiEndpointPrivate, directApiEndpoint = SelectCosApi(apiType, bLocation)
visibility = endpointType
if endpointType == "private" {
apiEndpoint = apiEndpointPrivate
Expand Down Expand Up @@ -1680,22 +1685,27 @@ func resourceIBMCOSBucketExists(d *schema.ResourceData, meta interface{}) (bool,
serviceID = bucketsatcrn
}

var apiEndpoint, apiEndpointPrivate, directApiEndpoint string
var apiEndpoint, apiEndpointPrivate, directApiEndpoint, visibility string
if apiType == "sl" {

apiEndpoint = SelectSatlocCosApi(apiType, serviceID, bLocation)

} else {
apiEndpoint, apiEndpointPrivate, directApiEndpoint = SelectCosApi(apiType, bLocation, false)
apiEndpoint, apiEndpointPrivate, directApiEndpoint = SelectCosApi(apiType, bLocation)
visibility = endpointType
if endpointType == "private" {
apiEndpoint = apiEndpointPrivate
}
if endpointType == "direct" {
// visibility type "direct" is not supported in endpoints file.
visibility = "private"
apiEndpoint = directApiEndpoint
}

}

apiEndpoint = conns.FileFallBack(rsConClient.Config.EndpointsFile, visibility, "IBMCLOUD_COS_ENDPOINT", bLocation, apiEndpoint)

apiEndpoint = conns.EnvFallBack([]string{"IBMCLOUD_COS_ENDPOINT"}, apiEndpoint)

if apiEndpoint == "" {
Expand Down Expand Up @@ -1740,11 +1750,8 @@ func resourceIBMCOSBucketExists(d *schema.ResourceData, meta interface{}) (bool,
return false, nil
}

func SelectCosApi(apiType string, bLocation string, test bool) (string, string, string) {
func SelectCosApi(apiType string, bLocation string) (string, string, string) {
hostUrl := "cloud-object-storage.appdomain.cloud"
if test {
hostUrl = "cloud-object-storage.test.appdomain.cloud"
}
if apiType == "crl" {
return fmt.Sprintf("s3.%s.%s", bLocation, hostUrl), fmt.Sprintf("s3.private.%s.%s", bLocation, hostUrl), fmt.Sprintf("s3.direct.%s.%s", bLocation, hostUrl)
}
Expand Down
7 changes: 2 additions & 5 deletions ibm/service/cos/resource_ibm_cos_bucket_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,12 +476,9 @@ func resourceIBMCOSBucketObjectDelete(ctx context.Context, d *schema.ResourceDat
return nil
}

func getCosEndpoint(bucketLocation string, endpointType string, test bool) string {
func getCosEndpoint(bucketLocation string, endpointType string) string {
if bucketLocation != "" {
hostUrl := "cloud-object-storage.appdomain.cloud"
if test {
hostUrl = "cloud-object-storage.test.appdomain.cloud"
}
switch endpointType {
case "public":
return fmt.Sprintf("s3.%s.%s", bucketLocation, hostUrl)
Expand All @@ -502,7 +499,7 @@ func getS3Client(bxSession *bxsession.Session, bucketLocation string, endpointTy
if endpointType == "direct" {
visibility = "private"
}
apiEndpoint := getCosEndpoint(bucketLocation, endpointType, false)
apiEndpoint := getCosEndpoint(bucketLocation, endpointType)
apiEndpoint = conns.FileFallBack(bxSession.Config.EndpointsFile, visibility, "IBMCLOUD_COS_ENDPOINT", bucketLocation, apiEndpoint)
apiEndpoint = conns.EnvFallBack([]string{"IBMCLOUD_COS_ENDPOINT"}, apiEndpoint)
if apiEndpoint == "" {
Expand Down
2 changes: 1 addition & 1 deletion ibm/service/cos/resource_ibm_cos_bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2242,7 +2242,7 @@ func testAccCheckIBMCosBucketExists(resource string, bucket string, regiontype s
rt = "crl"
}

apiEndpoint, _, _ := cos.SelectCosApi(rt, region, false)
apiEndpoint, _, _ := cos.SelectCosApi(rt, region)

rsContClient, err := acc.TestAccProvider.Meta().(conns.ClientSession).BluemixSession()
if err != nil {
Expand Down
7 changes: 2 additions & 5 deletions ibm/service/cos/resource_ibm_cos_replication_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,13 +334,10 @@ func parseBucketReplId(id string, info string) string {
return parseBucketId(bucketCRN, info)
}

func getCosEndpointType(bucketLocation string, endpointType string, test bool) string {
func getCosEndpointType(bucketLocation string, endpointType string) string {

if bucketLocation != "" {
hostUrl := "cloud-object-storage.appdomain.cloud"
if test {
hostUrl = "cloud-object-storage.test.appdomain.cloud"
}
switch endpointType {
case "public":
return fmt.Sprintf("s3.%s.%s", bucketLocation, hostUrl)
Expand All @@ -363,7 +360,7 @@ func getS3ClientSession(bxSession *bxsession.Session, bucketLocation string, end
if endpointType == "direct" {
visibility = "private"
}
apiEndpoint := getCosEndpointType(bucketLocation, endpointType, false)
apiEndpoint := getCosEndpointType(bucketLocation, endpointType)
apiEndpoint = conns.FileFallBack(bxSession.Config.EndpointsFile, visibility, "IBMCLOUD_COS_ENDPOINT", bucketLocation, apiEndpoint)
apiEndpoint = conns.EnvFallBack([]string{"IBMCLOUD_COS_ENDPOINT"}, apiEndpoint)
if apiEndpoint == "" {
Expand Down

0 comments on commit 446909f

Please sign in to comment.