From d2977cae0f166640ccc86cccaaa11eb54faf032e Mon Sep 17 00:00:00 2001 From: vinayhavalagi Date: Fri, 16 Sep 2022 20:30:20 +0530 Subject: [PATCH 1/4] appconfig snapshot support --- ibm/provider/provider.go | 3 + .../data_source_ibm_app_config_snapshot.go | 251 ++++++++++++ .../data_source_ibm_app_config_snapshots.go | 357 ++++++++++++++++++ .../resource_ibm_app_config_snapshot.go | 306 +++++++++++++++ .../resource_ibm_app_config_snapshot_test.go | 144 +++++++ .../docs/d/app_config_snapshot.html.markdown | 53 +++ .../docs/d/app_config_snapshots.html.markdown | 93 +++++ .../docs/r/app_config_snapshot.html.markdown | 68 ++++ 8 files changed, 1275 insertions(+) create mode 100644 ibm/service/appconfiguration/data_source_ibm_app_config_snapshot.go create mode 100644 ibm/service/appconfiguration/data_source_ibm_app_config_snapshots.go create mode 100644 ibm/service/appconfiguration/resource_ibm_app_config_snapshot.go create mode 100644 ibm/service/appconfiguration/resource_ibm_app_config_snapshot_test.go create mode 100644 website/docs/d/app_config_snapshot.html.markdown create mode 100644 website/docs/d/app_config_snapshots.html.markdown create mode 100644 website/docs/r/app_config_snapshot.html.markdown diff --git a/ibm/provider/provider.go b/ibm/provider/provider.go index 81e5dbe66a1..68c95547b62 100644 --- a/ibm/provider/provider.go +++ b/ibm/provider/provider.go @@ -505,6 +505,8 @@ func Provider() *schema.Provider { "ibm_app_config_features": appconfiguration.DataSourceIBMAppConfigFeatures(), "ibm_app_config_segment": appconfiguration.DataSourceIBMAppConfigSegment(), "ibm_app_config_segments": appconfiguration.DataSourceIBMAppConfigSegments(), + "ibm_app_config_snapshot": appconfiguration.DataSourceIBMAppConfigSnapshot(), + "ibm_app_config_snapshots": appconfiguration.DataSourceIBMAppConfigSnapshots(), "ibm_resource_quota": resourcecontroller.DataSourceIBMResourceQuota(), "ibm_resource_group": resourcemanager.DataSourceIBMResourceGroup(), @@ -941,6 +943,7 @@ func Provider() *schema.Provider { "ibm_app_config_environment": appconfiguration.ResourceIBMAppConfigEnvironment(), "ibm_app_config_feature": appconfiguration.ResourceIBMIbmAppConfigFeature(), "ibm_app_config_segment": appconfiguration.ResourceIBMIbmAppConfigSegment(), + "ibm_app_config_snapshot": appconfiguration.ResourceIBMIbmAppConfigSnapshot(), "ibm_kms_key": kms.ResourceIBMKmskey(), "ibm_kms_key_alias": kms.ResourceIBMKmskeyAlias(), "ibm_kms_key_rings": kms.ResourceIBMKmskeyRings(), diff --git a/ibm/service/appconfiguration/data_source_ibm_app_config_snapshot.go b/ibm/service/appconfiguration/data_source_ibm_app_config_snapshot.go new file mode 100644 index 00000000000..bf1337e414a --- /dev/null +++ b/ibm/service/appconfiguration/data_source_ibm_app_config_snapshot.go @@ -0,0 +1,251 @@ +package appconfiguration + +import ( + "fmt" + "github.com/IBM/appconfiguration-go-admin-sdk/appconfigurationv1" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func DataSourceIBMAppConfigSnapshot() *schema.Resource { + return &schema.Resource{ + Read: dataSourceIbmAppConfigSnapshotRead, + + Schema: map[string]*schema.Schema{ + "guid": { + Type: schema.TypeString, + Required: true, + Description: "GUID of the App Configuration service. Get it from the service instance credentials section of the dashboard.", + }, + "git_config_id": { + Type: schema.TypeString, + Required: true, + Description: "Git config id. Allowed special characters are dot ( . ), hyphen( - ), underscore ( _ ) only", + }, + "git_config_name": { + Type: schema.TypeString, + Optional: true, + Description: "Git config name. Allowed special characters are dot ( . ), hyphen( - ), underscore ( _ ) only", + }, + "git_url": { + Type: schema.TypeString, + Optional: true, + Description: "Git url which will be used to connect to the github account.", + }, + "git_branch": { + Type: schema.TypeString, + Optional: true, + Description: "Branch name to which you need to write or update the configuration.", + }, + "git_file_path": { + Type: schema.TypeString, + Optional: true, + Description: "Git file path, this is a path where your configuration file will be written.", + }, + "created_time": { + Type: schema.TypeString, + Computed: true, + Description: "Creation time of the git config.", + }, + "updated_time": { + Type: schema.TypeString, + Computed: true, + Description: "Last modified time of the git config data.", + }, + "last_sync_time": { + Type: schema.TypeString, + Computed: true, + Description: "Latest time when the snapshot was synced to git.", + }, + "href": { + Type: schema.TypeString, + Computed: true, + Description: "Git config URL.", + }, + "collection": { + Type: schema.TypeList, + Computed: true, + Description: "Collection object.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "collection_name": { + Type: schema.TypeString, + Computed: true, + Description: "Collection name.", + }, + "collection_id": { + Type: schema.TypeString, + Computed: true, + Description: "Collection id.", + }, + }, + }, + }, + "environment": { + Type: schema.TypeList, + Computed: true, + Description: "Environment object", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "environment_name": { + Type: schema.TypeString, + Computed: true, + Description: "Environment name.", + }, + "environment_id": { + Type: schema.TypeString, + Computed: true, + Description: "Environment id.", + }, + "color_code": { + Type: schema.TypeString, + Computed: true, + Description: "Environment color code.", + }, + }, + }, + }, + }, + } +} + +func dataSourceIbmAppConfigSnapshotRead(d *schema.ResourceData, meta interface{}) error { + guid := d.Get("guid").(string) + + appconfigClient, err := getAppConfigClient(meta, guid) + if err != nil { + return fmt.Errorf("getAppConfigClient failed %s", err) + } + + options := &appconfigurationv1.GetGitconfigOptions{} + options.SetGitConfigID(d.Get("git_config_id").(string)) + + result, response, err := appconfigClient.GetGitconfig(options) + + if err != nil { + return fmt.Errorf("GetGitconfig failed %s\n%s", err, response) + } + + d.SetId(fmt.Sprintf("%s/%s", guid, *result.GitConfigID)) + + if result.GitConfigName != nil { + if err = d.Set("git_config_name", result.GitConfigName); err != nil { + return fmt.Errorf("[ERROR] Error setting git_config_name: %s", err) + } + } + if result.GitConfigID != nil { + if err = d.Set("git_config_id", result.GitConfigID); err != nil { + return fmt.Errorf("[ERROR] Error setting git_config_id: %s", err) + } + } + if result.GitURL != nil { + if err = d.Set("git_url", result.GitURL); err != nil { + return fmt.Errorf("[ERROR] Error setting git_url: %s", err) + } + } + if result.GitBranch != nil { + if err = d.Set("git_branch", result.GitBranch); err != nil { + return fmt.Errorf("[ERROR] Error setting git_branch: %s", err) + } + } + if result.GitFilePath != nil { + if err = d.Set("git_file_path", result.GitFilePath); err != nil { + return fmt.Errorf("[ERROR] Error setting git_file_path: %s", err) + } + } + if result.CreatedTime != nil { + if err = d.Set("created_time", result.CreatedTime.String()); err != nil { + return fmt.Errorf("[ERROR] Error setting created_time: %s", err) + } + } + if result.UpdatedTime != nil { + if err = d.Set("updated_time", result.UpdatedTime.String()); err != nil { + return fmt.Errorf("[ERROR] Error setting updated_time: %s", err) + } + } + if result.LastSyncTime != nil { + if err = d.Set("last_sync_time", result.LastSyncTime.String()); err != nil { + return fmt.Errorf("[ERROR] Error setting last_sync_time: %s", err) + } + } + if result.Href != nil { + if err = d.Set("href", result.Href); err != nil { + return fmt.Errorf("[ERROR] Error setting href: %s", err) + } + } + if result.Collection != nil { + collectionItemMap := resourceIbmAppConfigSnapshotCollectionRefToMap(result.Collection) + if err = d.Set("collection", collectionItemMap); err != nil { + return fmt.Errorf("[ERROR] Error setting collection: %s", err) + } + } + if result.Environment != nil { + environmentItemMap := resourceIbmAppConfigSnapshotEnvironmentRefToMap(result.Environment) + if err = d.Set("environment", environmentItemMap); err != nil { + return fmt.Errorf("[ERROR] Error setting environment: %s", err) + } + } + return nil +} + +type CollectionRef map[string]interface{} + +type Collections struct { + Collection_name string `json:"name"` + Collection_id string `json:"collection_id"` +} + +func resourceIbmAppConfigSnapshotCollectionRefToMap(collectionRef interface{}) []CollectionRef { + collections := getSnapshotCollection(collectionRef) + collectionRefMap := CollectionRef{} + var collectionMap []CollectionRef + collectionRefMap["collection_id"] = collections.Collection_id + collectionRefMap["collection_name"] = collections.Collection_name + collectionMap = append(collectionMap, collectionRefMap) + return collectionMap +} + +func getSnapshotCollection(data interface{}) Collections { + m := data.(map[string]interface{}) + collection := Collections{} + if name, ok := m["name"].(string); ok { + collection.Collection_name = name + } + if id, ok := m["collection_id"].(string); ok { + collection.Collection_id = id + } + return collection +} + +type EnvironmentRef map[string]interface{} + +type Environments struct { + Environment_name string `json:"name"` + Environment_id string `json:"environment_id"` + Color_code string `json:"color_code"` +} + +func resourceIbmAppConfigSnapshotEnvironmentRefToMap(environmentRef interface{}) []EnvironmentRef { + environments := getSnapshotEnvironment(environmentRef) + environmentRefMap := EnvironmentRef{} + var environmentMap []EnvironmentRef + environmentRefMap["environment_id"] = environments.Environment_id + environmentRefMap["environment_name"] = environments.Environment_name + environmentRefMap["color_code"] = environments.Color_code + environmentMap = append(environmentMap, environmentRefMap) + return environmentMap +} + +func getSnapshotEnvironment(data interface{}) Environments { + m := data.(map[string]interface{}) + environment := Environments{} + if name, ok := m["name"].(string); ok { + environment.Environment_name = name + } + if id, ok := m["environment_id"].(string); ok { + environment.Environment_id = id + } + if color, ok := m["color_code"].(string); ok { + environment.Color_code = color + } + return environment +} diff --git a/ibm/service/appconfiguration/data_source_ibm_app_config_snapshots.go b/ibm/service/appconfiguration/data_source_ibm_app_config_snapshots.go new file mode 100644 index 00000000000..68c5941df6c --- /dev/null +++ b/ibm/service/appconfiguration/data_source_ibm_app_config_snapshots.go @@ -0,0 +1,357 @@ +package appconfiguration + +import ( + "fmt" + "net/url" + "reflect" + "strconv" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + + "github.com/IBM/appconfiguration-go-admin-sdk/appconfigurationv1" +) + +func DataSourceIBMAppConfigSnapshots() *schema.Resource { + return &schema.Resource{ + Read: dataSourceIbmAppConfigSnapshotsRead, + + Schema: map[string]*schema.Schema{ + "guid": { + Type: schema.TypeString, + Required: true, + Description: "GUID of the App Configuration service. Get it from the service instance credentials section of the dashboard.", + }, + "collection_id": { + Type: schema.TypeString, + Optional: true, + Description: "Filters the response based on the specified collection_id.", + }, + "environment_id": { + Type: schema.TypeString, + Optional: true, + Description: "Filters the response based on the specified environment_id.", + }, + "limit": { + Type: schema.TypeInt, + Optional: true, + Description: "The number of records to retrieve. By default, the list operation return the first 10 records. To retrieve different set of records, use `limit` with `offset` to page through the available records.", + }, + "offset": { + Type: schema.TypeInt, + Optional: true, + Description: "The number of records to skip. By specifying `offset`, you retrieve a subset of items that starts with the `offset` value. Use `offset` with `limit` to page through the available records.", + }, + "total_count": { + Type: schema.TypeInt, + Computed: true, + Description: "Total number of records.", + }, + "git_config": { + Type: schema.TypeList, + Computed: true, + Description: "Array of git_config.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "git_config_name": { + Type: schema.TypeString, + Optional: true, + Description: "Git config name.", + }, + "git_config_id": { + Type: schema.TypeString, + Optional: true, + Description: "Git config id", + }, + "git_url": { + Type: schema.TypeString, + Optional: true, + Description: "Git url which will be used to connect to the github account", + }, + "git_branch": { + Type: schema.TypeString, + Optional: true, + Description: "Git url which will be used to connect to the github account", + }, + "git_file_path": { + Type: schema.TypeString, + Optional: true, + Description: "Git file path, this is a path where your configuration file will be written.", + }, + "created_time": { + Type: schema.TypeString, + Computed: true, + Description: "Creation time of the git config.", + }, + "updated_time": { + Type: schema.TypeString, + Computed: true, + Description: "Last modified time of the git config data.", + }, + "last_sync_time": { + Type: schema.TypeString, + Computed: true, + Description: "Latest time when the snapshot was synced to git.", + }, + "href": { + Type: schema.TypeString, + Computed: true, + Description: "Git config URL.", + }, + "collection": { + Type: schema.TypeList, + Computed: true, + Description: "Collection object.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "collection_name": { + Type: schema.TypeString, + Computed: true, + Description: "Collection name.", + }, + "collection_id": { + Type: schema.TypeString, + Computed: true, + Description: "Collection id.", + }, + }, + }, + }, + "environment": { + Type: schema.TypeList, + Computed: true, + Description: "Environment object", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "environment_name": { + Type: schema.TypeString, + Computed: true, + Description: "Environment name.", + }, + "environment_id": { + Type: schema.TypeString, + Computed: true, + Description: "Environment id.", + }, + "color_code": { + Type: schema.TypeString, + Computed: true, + Description: "Environment color code.", + }, + }, + }, + }, + }, + }, + }, + }, + } +} + +func dataSourceIbmAppConfigSnapshotsRead(d *schema.ResourceData, meta interface{}) error { + guid := d.Get("guid").(string) + + appconfigClient, err := getAppConfigClient(meta, guid) + if err != nil { + return fmt.Errorf("getAppConfigClient failed %s", err) + } + + options := &appconfigurationv1.ListSnapshotsOptions{} + + if _, ok := d.GetOk("collection_id"); ok { + options.SetCollectionID(d.Get("collection_id").(string)) + } + if _, ok := d.GetOk("environment_id"); ok { + options.SetEnvironmentID(d.Get("environment_id").(string)) + } + if _, ok := d.GetOk("search"); ok { + options.SetSearch(d.Get("search").(string)) + } + if _, ok := d.GetOk("sort"); ok { + options.SetSort(d.Get("sort").(string)) + } + + var shapshotsList *appconfigurationv1.SnapshotsList + var offset int64 + var limit int64 = 10 + var isLimit bool + + finalList := []appconfigurationv1.SnapshotResponseGetApi{} + + if _, ok := d.GetOk("limit"); ok { + isLimit = true + limit = int64(d.Get("limit").(int)) + } + options.SetLimit(limit) + if _, ok := d.GetOk("offset"); ok { + offset = int64(d.Get("offset").(int)) + } + + for { + options.Offset = &offset + result, response, err := appconfigClient.ListSnapshots(options) + shapshotsList = result + if err != nil { + return fmt.Errorf("ListSnapshots failed %s\n%s", err, response) + } + if isLimit { + offset = 0 + } else { + offset = dataSourceSnapshotsListGetNext(result.Next) + } + finalList = append(finalList, result.Snapshot...) + if offset == 0 { + break + } + } + + shapshotsList.Snapshot = finalList + + d.SetId(fmt.Sprintf("%s", guid)) + + if shapshotsList.Snapshot != nil { + err = d.Set("git_config", dataSourceFeaturesListFlattenSnapshots(shapshotsList.Snapshot)) + if err != nil { + return fmt.Errorf("[ERROR] Error setting git_config %s", err) + } + } + if shapshotsList.TotalCount != nil { + if err = d.Set("total_count", shapshotsList.TotalCount); err != nil { + return fmt.Errorf("[ERROR] Error setting total_count: %s", err) + } + } + if shapshotsList.Limit != nil { + if err = d.Set("limit", shapshotsList.Limit); err != nil { + return fmt.Errorf("[ERROR] Error setting limit: %s", err) + } + } + if shapshotsList.Offset != nil { + if err = d.Set("offset", shapshotsList.Offset); err != nil { + return fmt.Errorf("[ERROR] Error setting offset: %s", err) + } + } + + return nil +} + +func dataSourceSnapshotsListGetNext(next interface{}) int64 { + if reflect.ValueOf(next).IsNil() { + return 0 + } + + u, err := url.Parse(reflect.ValueOf(next).Elem().FieldByName("Href").Elem().String()) + if err != nil { + return 0 + } + + q := u.Query() + var page string + + if q.Get("start") != "" { + page = q.Get("start") + } else if q.Get("offset") != "" { + page = q.Get("offset") + } + + convertedVal, err := strconv.ParseInt(page, 10, 64) + if err != nil { + return 0 + } + return convertedVal +} + +func dataSourceFeaturesListFlattenSnapshots(result []appconfigurationv1.SnapshotResponseGetApi) (snapshots []map[string]interface{}) { + for _, snapshotsItem := range result { + snapshots = append(snapshots, dataSourceSnapshotsListSnapshotsToMap(snapshotsItem)) + } + return snapshots +} + +func dataSourceSnapshotsListSnapshotsToMap(snapshotsItem appconfigurationv1.SnapshotResponseGetApi) (snapshotsMap map[string]interface{}) { + snapshotsMap = map[string]interface{}{} + + if snapshotsItem.GitConfigName != nil { + snapshotsMap["git_config_name"] = snapshotsItem.GitConfigName + } + if snapshotsItem.GitConfigID != nil { + snapshotsMap["git_config_id"] = snapshotsItem.GitConfigID + } + if snapshotsItem.GitURL != nil { + snapshotsMap["git_url"] = snapshotsItem.GitURL + } + if snapshotsItem.GitBranch != nil { + snapshotsMap["git_branch"] = snapshotsItem.GitBranch + } + if snapshotsItem.GitFilePath != nil { + snapshotsMap["git_file_path"] = snapshotsItem.GitFilePath + } + if snapshotsItem.CreatedTime != nil { + snapshotsMap["created_time"] = snapshotsItem.CreatedTime.String() + } + if snapshotsItem.UpdatedTime != nil { + snapshotsMap["updated_time"] = snapshotsItem.UpdatedTime.String() + } + if snapshotsItem.LastSyncTime != nil { + snapshotsMap["last_sync_time"] = snapshotsItem.LastSyncTime.String() + } + if snapshotsItem.Href != nil { + snapshotsMap["href"] = snapshotsItem.Href + } + + if snapshotsItem.Collection != nil { + collectionItemMap := resourceIbmAppConfigSnapshotsCollectionRefToMap(snapshotsItem.Collection) + snapshotsMap["collection"] = collectionItemMap + } + if snapshotsItem.Environment != nil { + environmentItemMap := resourceIbmAppConfigSnapshotsEnvironmentRefToMap(snapshotsItem.Environment) + snapshotsMap["environment"] = environmentItemMap + } + return snapshotsMap +} + +func resourceIbmAppConfigSnapshotsCollectionRefToMap(collectionRef interface{}) []CollectionRef { + collections := getSnapshotsCollection(collectionRef) + collectionRefMap := CollectionRef{} + var collectionMap []CollectionRef + collectionRefMap["collection_id"] = collections.Collection_id + collectionRefMap["collection_name"] = collections.Collection_name + collectionMap = append(collectionMap, collectionRefMap) + return collectionMap +} + +func getSnapshotsCollection(data interface{}) Collections { + m := data.(map[string]interface{}) + collection := Collections{} + if name, ok := m["name"].(string); ok { + collection.Collection_name = name + } + if id, ok := m["collection_id"].(string); ok { + collection.Collection_id = id + } + return collection +} + +func resourceIbmAppConfigSnapshotsEnvironmentRefToMap(environmentRef interface{}) []EnvironmentRef { + environments := getSnapshotsEnvironment(environmentRef) + environmentRefMap := EnvironmentRef{} + var environmentMap []EnvironmentRef + environmentRefMap["environment_id"] = environments.Environment_id + environmentRefMap["environment_name"] = environments.Environment_name + environmentRefMap["color_code"] = environments.Color_code + environmentMap = append(environmentMap, environmentRefMap) + return environmentMap +} + +func getSnapshotsEnvironment(data interface{}) Environments { + m := data.(map[string]interface{}) + environment := Environments{} + if name, ok := m["name"].(string); ok { + environment.Environment_name = name + } + if id, ok := m["environment_id"].(string); ok { + environment.Environment_id = id + } + if color, ok := m["color_code"].(string); ok { + environment.Color_code = color + } + return environment +} diff --git a/ibm/service/appconfiguration/resource_ibm_app_config_snapshot.go b/ibm/service/appconfiguration/resource_ibm_app_config_snapshot.go new file mode 100644 index 00000000000..13570003624 --- /dev/null +++ b/ibm/service/appconfiguration/resource_ibm_app_config_snapshot.go @@ -0,0 +1,306 @@ +package appconfiguration + +import ( + "fmt" + "github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex" + "github.com/IBM/appconfiguration-go-admin-sdk/appconfigurationv1" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "log" +) + +func ResourceIBMIbmAppConfigSnapshot() *schema.Resource { + return &schema.Resource{ + Create: resourceIbmIbmAppConfigSnapshotCreate, + Read: resourceIbmIbmAppConfigSnapshotRead, + Update: resourceIbmIbmAppConfigSnapshotUpdate, + Delete: resourceIbmIbmAppConfigSnapshotDelete, + Importer: &schema.ResourceImporter{}, + + Schema: map[string]*schema.Schema{ + "guid": { + Type: schema.TypeString, + Required: true, + Description: "GUID of the App Configuration service. Get it from the service instance credentials section of the dashboard.", + }, + "git_config_id": { + Type: schema.TypeString, + Required: true, + Description: "Git config id. Allowed special characters are dot ( . ), hyphen( - ), underscore ( _ ) only", + }, + "git_config_name": { + Type: schema.TypeString, + Required: true, + Description: "Git config name. Allowed special characters are dot ( . ), hyphen( - ), underscore ( _ ) only", + }, + "git_url": { + Type: schema.TypeString, + Required: true, + Description: "Git url which will be used to connect to the github account.", + }, + "git_branch": { + Type: schema.TypeString, + Required: true, + Description: "Branch name to which you need to write or update the configuration.", + }, + "git_file_path": { + Type: schema.TypeString, + Required: true, + Description: "Git file path, this is a path where your configuration file will be written.", + }, + "git_token": { + Type: schema.TypeString, + Required: true, + Description: "Git token, this needs to be provided with enough permission to write and update the file.", + }, + "created_time": { + Type: schema.TypeString, + Computed: true, + Description: "Creation time of the git config.", + }, + "collection_id": { + Type: schema.TypeString, + Required: true, + Description: "Collection id.", + }, + "action": { + Type: schema.TypeString, + Optional: true, + Description: "action promote", + }, + "environment_id": { + Type: schema.TypeString, + Required: true, + Description: "Environment id.", + }, + "updated_time": { + Type: schema.TypeString, + Computed: true, + Description: "Last modified time of the git config data.", + }, + "href": { + Type: schema.TypeString, + Computed: true, + Description: "Git config URL.", + }, + "collection": { + Type: schema.TypeList, + Computed: true, + Description: "Collection object.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "collection_name": { + Type: schema.TypeString, + Computed: true, + Description: "Collection name.", + }, + "collection_id": { + Type: schema.TypeString, + Computed: true, + Description: "Collection id.", + }, + }, + }, + }, + "environment": { + Type: schema.TypeList, + Computed: true, + Description: "Environment object", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "environment_name": { + Type: schema.TypeString, + Computed: true, + Description: "Environment name.", + }, + "environment_id": { + Type: schema.TypeString, + Computed: true, + Description: "Environment id.", + }, + "color_code": { + Type: schema.TypeString, + Computed: true, + Description: "Environment color code.", + }, + }, + }, + }, + }, + } +} + +func resourceIbmIbmAppConfigSnapshotCreate(d *schema.ResourceData, meta interface{}) error { + + guid := d.Get("guid").(string) + appconfigClient, err := getAppConfigClient(meta, guid) + if err != nil { + return err + } + options := &appconfigurationv1.CreateGitconfigOptions{} + + options.SetGitConfigName(d.Get("git_config_name").(string)) + options.SetGitConfigID(d.Get("git_config_id").(string)) + + options.SetCollectionID(d.Get("collection_id").(string)) + options.SetEnvironmentID(d.Get("environment_id").(string)) + options.SetGitURL(d.Get("git_url").(string)) + options.SetGitBranch(d.Get("git_branch").(string)) + options.SetGitFilePath(d.Get("git_file_path").(string)) + options.SetGitToken(d.Get("git_token").(string)) + + snapshot, response, err := appconfigClient.CreateGitconfig(options) + + if err != nil { + return fmt.Errorf("CreateGitconfig failed %s\n%s", err, response) + } + d.SetId(fmt.Sprintf("%s/%s", guid, *snapshot.GitConfigID)) + return resourceIbmIbmAppConfigSnapshotRead(d, meta) +} + +func resourceIbmIbmAppConfigSnapshotUpdate(d *schema.ResourceData, meta interface{}) error { + parts, err := flex.IdParts(d.Id()) + if err != nil { + return nil + } + appconfigClient, err := getAppConfigClient(meta, parts[0]) + if err != nil { + return err + } + + if ok := d.HasChanges("action"); ok { + option := &appconfigurationv1.PromoteGitconfigOptions{} + option.SetGitConfigID(parts[1]) + _, response, err := appconfigClient.PromoteGitconfig(option) + if err != nil { + log.Printf("[DEBUG] PromoteGitconfig %s\n%s", err, response) + return err + } + return resourceIbmIbmAppConfigSnapshotRead(d, meta) + } else { + if ok := d.HasChanges("git_config_name", "collection_id", "environment_id", "git_url", "git_branch", "git_file_path", "git_token"); ok { + options := &appconfigurationv1.UpdateGitconfigOptions{} + options.SetGitConfigID(parts[1]) + if _, ok := d.GetOk("git_config_name"); ok { + options.SetGitConfigName(d.Get("git_config_name").(string)) + } + if _, ok := d.GetOk("collection_id"); ok { + options.SetCollectionID(d.Get("collection_id").(string)) + } + if _, ok := d.GetOk("environment_id"); ok { + options.SetEnvironmentID(d.Get("environment_id").(string)) + } + if _, ok := d.GetOk("git_url"); ok { + options.SetGitURL(d.Get("git_url").(string)) + } + if _, ok := d.GetOk("git_branch"); ok { + options.SetGitBranch(d.Get("git_branch").(string)) + } + if _, ok := d.GetOk("git_file_path"); ok { + options.SetGitFilePath(d.Get("git_file_path").(string)) + } + if _, ok := d.GetOk("git_token"); ok { + options.SetGitToken(d.Get("git_token").(string)) + } + _, response, err := appconfigClient.UpdateGitconfig(options) + if err != nil { + log.Printf("[DEBUG] UpdateGitconfig %s\n%s", err, response) + return err + } + return resourceIbmIbmAppConfigSnapshotRead(d, meta) + } + } + return nil +} + +func resourceIbmIbmAppConfigSnapshotRead(d *schema.ResourceData, meta interface{}) error { + parts, err := flex.IdParts(d.Id()) + if err != nil { + return nil + } + appconfigClient, err := getAppConfigClient(meta, parts[0]) + if err != nil { + return err + } + if len(parts) != 2 { + return fmt.Errorf("Kindly check the id") + } + + options := &appconfigurationv1.GetGitconfigOptions{} + options.SetGitConfigID(parts[1]) + + result, response, err := appconfigClient.GetGitconfig(options) + if err != nil { + return fmt.Errorf("[DEBUG] GetGitconfigs failed %s\n%s", err, response) + } + + d.Set("guid", parts[0]) + d.Set("git_config_id", parts[1]) + if result.GitConfigName != nil { + if err = d.Set("git_config_name", result.GitConfigName); err != nil { + return fmt.Errorf("[ERROR] Error setting git_config_name: %s", err) + } + } + if result.GitConfigID != nil { + if err = d.Set("git_config_id", result.GitConfigID); err != nil { + return fmt.Errorf("[ERROR] Error setting git_config_id: %s", err) + } + } + if result.GitURL != nil { + if err = d.Set("git_url", result.GitURL); err != nil { + return fmt.Errorf("[ERROR] Error setting git_url: %s", err) + } + } + if result.GitBranch != nil { + if err = d.Set("git_branch", result.GitBranch); err != nil { + return fmt.Errorf("[ERROR] Error setting git_branch: %s", err) + } + } + if result.GitFilePath != nil { + if err = d.Set("git_file_path", result.GitFilePath); err != nil { + return fmt.Errorf("[ERROR] Error setting git_file_path: %s", err) + } + } + if result.CreatedTime != nil { + if err = d.Set("created_time", result.CreatedTime.String()); err != nil { + return fmt.Errorf("[ERROR] Error setting created_time: %s", err) + } + } + if result.UpdatedTime != nil { + if err = d.Set("updated_time", result.UpdatedTime.String()); err != nil { + return fmt.Errorf("[ERROR] Error setting updated_time: %s", err) + } + } + if result.Href != nil { + if err = d.Set("href", result.Href); err != nil { + return fmt.Errorf("[ERROR] Error setting href: %s", err) + } + } + return nil +} + +func resourceIbmIbmAppConfigSnapshotDelete(d *schema.ResourceData, meta interface{}) error { + parts, err := flex.IdParts(d.Id()) + if err != nil { + return nil + } + appconfigClient, err := getAppConfigClient(meta, parts[0]) + if err != nil { + return err + } + + options := &appconfigurationv1.DeleteGitconfigOptions{} + options.SetGitConfigID(parts[1]) + + response, err := appconfigClient.DeleteGitconfig(options) + if err != nil { + if response != nil && response.StatusCode == 404 { + d.SetId("") + return nil + } + return fmt.Errorf("[DEBUG] DeleteGitconfig failed %s\n%s", err, response) + } + d.SetId("") + + return nil +} diff --git a/ibm/service/appconfiguration/resource_ibm_app_config_snapshot_test.go b/ibm/service/appconfiguration/resource_ibm_app_config_snapshot_test.go new file mode 100644 index 00000000000..661133de68c --- /dev/null +++ b/ibm/service/appconfiguration/resource_ibm_app_config_snapshot_test.go @@ -0,0 +1,144 @@ +package appconfiguration_test + +import ( + "fmt" + acc "github.com/IBM-Cloud/terraform-provider-ibm/ibm/acctest" + "github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" + "testing" + + "github.com/IBM/appconfiguration-go-admin-sdk/appconfigurationv1" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" +) + +func TestAccIbmIbmAppConfigSnapshotBasic(t *testing.T) { + var conf appconfigurationv1.SnapshotResponseGetApi + instanceName := fmt.Sprintf("tf_app_config_test_%d", acctest.RandIntRange(10, 100)) + git_config_name := fmt.Sprintf("tf_git_config_name_%d", acctest.RandIntRange(10, 100)) + git_config_id := fmt.Sprintf("tf_git_config_id_%d", acctest.RandIntRange(10, 100)) + git_url := fmt.Sprintf("tf_git_url_%d", acctest.RandIntRange(10, 100)) + git_branch := fmt.Sprintf("tf_git_branch_%d", acctest.RandIntRange(10, 100)) + git_file_path := fmt.Sprintf("tf_git_file_path_%d", acctest.RandIntRange(10, 100)) + git_token := fmt.Sprintf("tf_git_token_%d", acctest.RandIntRange(10, 100)) + collection_id := fmt.Sprintf("tf_collection_id_%d", acctest.RandIntRange(10, 100)) + git_config_nameUpdate := fmt.Sprintf("tf_git_config_name_%d", acctest.RandIntRange(10, 100)) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { acc.TestAccPreCheck(t) }, + Providers: acc.TestAccProviders, + CheckDestroy: testAccCheckIbmAppConfigSnapshotDestroy, + Steps: []resource.TestStep{ + { + Config: testAccCheckIbmAppConfigSnapshotConfigBasic(instanceName, git_config_name, git_config_id, git_url, git_branch, git_file_path, + git_token, collection_id), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckIbmAppConfigSnapshotExists("ibm_app_config_snapshot.ibm_app_config_snapshot_resource1", conf), + resource.TestCheckResourceAttrSet("ibm_app_config_snapshot.ibm_app_config_snapshot_resource1", "environment_id"), + resource.TestCheckResourceAttrSet("ibm_app_config_snapshot.ibm_app_config_snapshot_resource1", "name"), + resource.TestCheckResourceAttrSet("ibm_app_config_snapshot.ibm_app_config_snapshot_resource1", "property_id"), + resource.TestCheckResourceAttrSet("ibm_app_config_snapshot.ibm_app_config_snapshot_resource1", "type"), + resource.TestCheckResourceAttrSet("ibm_app_config_snapshot.ibm_app_config_snapshot_resource1", "description"), + resource.TestCheckResourceAttrSet("ibm_app_config_snapshot.ibm_app_config_snapshot_resource1", "tags"), + resource.TestCheckResourceAttrSet("ibm_app_config_snapshot.ibm_app_config_snapshot_resource1", "created_time"), + resource.TestCheckResourceAttrSet("ibm_app_config_snapshot.ibm_app_config_snapshot_resource1", "updated_time"), + resource.TestCheckResourceAttrSet("ibm_app_config_snapshot.ibm_app_config_snapshot_resource1", "href"), + resource.TestCheckResourceAttrSet("ibm_app_config_snapshot.ibm_app_config_snapshot_resource1", "segment_exists"), + ), + }, + { + Config: testAccCheckIbmAppConfigSnapshotConfigBasic(instanceName, git_config_nameUpdate, git_config_id, git_url, git_branch, git_file_path, + git_token, collection_id), + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr("ibm_app_config_snapshot.ibm_app_config_snapshot_resource1", "name", git_config_nameUpdate), + ), + }, + }, + }) + +} + +func testAccCheckIbmAppConfigSnapshotConfigBasic(instanceName, git_config_id, git_config_name, git_url, git_branch, git_file_path, + git_token, collection_id string) string { + return fmt.Sprintf(` + resource "ibm_resource_instance" "app_config_terraform_test476" { + name = "%s" + location = "us-south" + service = "apprapp" + plan = "lite" + } + resource "ibm_app_config_snapshot" "ibm_app_config_property_resource1" { + guid = ibm_resource_instance.app_config_terraform_test476.guid + git_config_id = "%s" + git_config_name = "%s" + git_url = "%s" + git_branch = "%s" + git_file_path = "%s" + git_token = "%s" + collection_id = "%s" + environment_id = "dev" + }`, instanceName, git_config_id, git_config_name, git_url, git_branch, git_file_path, + git_token, collection_id) +} + +func testAccCheckIbmAppConfigSnapshotExists(n string, obj appconfigurationv1.SnapshotResponseGetApi) resource.TestCheckFunc { + + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[n] + if !ok { + return fmt.Errorf("Not found: %s", n) + } + + parts, err := flex.IdParts(rs.Primary.ID) + if err != nil { + return err + } + + appconfigClient, err := getAppConfigClient(acc.TestAccProvider.Meta(), parts[0]) + if err != nil { + return err + } + options := &appconfigurationv1.GetGitconfigOptions{} + + options.SetGitConfigID(parts[1]) + + snapshot, _, err := appconfigClient.GetGitconfig(options) + if err != nil { + return err + } + + obj = *snapshot + return nil + } +} + +func testAccCheckIbmAppConfigSnapshotDestroy(s *terraform.State) error { + for _, rs := range s.RootModule().Resources { + if rs.Type != "ibm_app_config_snapshot" { + continue + } + parts, err := flex.IdParts(rs.Primary.ID) + if err != nil { + return err + } + + appconfigClient, err := getAppConfigClient(acc.TestAccProvider.Meta(), parts[0]) + if err != nil { + return err + } + options := &appconfigurationv1.GetGitconfigOptions{} + + options.SetGitConfigID(parts[1]) + + // Try to find the key + _, response, err := appconfigClient.GetGitconfig(options) + + if err == nil { + return fmt.Errorf("app_config_snapshot still exists: %s", rs.Primary.ID) + } else if response.StatusCode != 404 { + return fmt.Errorf("Error checking for app_config_snapshot (%s) has been destroyed: %s", rs.Primary.ID, err) + } + } + + return nil +} diff --git a/website/docs/d/app_config_snapshot.html.markdown b/website/docs/d/app_config_snapshot.html.markdown new file mode 100644 index 00000000000..e2b3be7717a --- /dev/null +++ b/website/docs/d/app_config_snapshot.html.markdown @@ -0,0 +1,53 @@ +--- +subcategory: 'App Configuration' +layout: 'ibm' +page_title: 'IBM : App Configuration Snapshot' +description: |- + Get information about Snapshot +--- + +# ibm_app_config_segment +Retrieve information about an existing IBM Cloud App Configuration snapshot. You can then reference the fields of the data source in other resources within the same configuration using interpolation syntax. For more information, about App Configuration snapshot, see [App Configuration concepts](https://cloud.ibm.com//docs/app-configuration?topic=app-configuration-ac-overview). + +## Example usage + +```terraform +data "ibm_app_config_snapshot" "app_config_snapshot_read" { + guid = "guid" + git_config_id = "git_config_id" +} +``` + +## Argument reference + +Review the argument reference that you can specify for your data source. + +- `guid` - (Required, String) The GUID of the App Configuration service. Get it from the service instance credentials section of the dashboard. +- `git_config_id` - (Required, String) The Git Config Id. + + +## Attribute reference + +In addition to all argument references list, you can access the following attribute references after your resource is created. + +- `git_config_name` - (String) Git config name. Allowed special characters are dot ( . ), hyphen( - ), underscore ( _ ) only. +- `git_config_id` - (String) Git config id. Allowed special characters are dot ( . ), hyphen( - ), underscore ( _ ) only +- `git_url` - (String) Git url which will be used to connect to the github account. The url must be formed in this format, https://api.github.com/repos/{owner}/{repo_name} for the personal git account. +- `git_branch` - (String) Branch name to which you need to write or update the configuration. +- `git_file_path` - (String) Git file path, this is a path where your configuration file will be written. The path must contain the file name with `json` extension. +- `collection` - (Object) Collection object will be returned containing attributes collection_id, collection_name. + + Nested scheme for `collection`: + - `collection_id` - (String) Collection id. + - `name` - (String) Name of the collection. + +- `environment` - (Object) Environment object will be returned containing attributes environment_id, environment_name, color_code. + + Nested scheme for `environment` : + - `environment_id` - (String) Environment Id. + - `environment_name` - (String) Environment name. + +- `created_time` - (Timestamp) Creation time of the git config. +- `updated_time` - (Timestamp) Last modified time of the git config data. +- `last_sync_time` - (Timestamp) Latest time when the snapshot was synced to git. +- `href` - (String) Git config URL. diff --git a/website/docs/d/app_config_snapshots.html.markdown b/website/docs/d/app_config_snapshots.html.markdown new file mode 100644 index 00000000000..706d6c32ff9 --- /dev/null +++ b/website/docs/d/app_config_snapshots.html.markdown @@ -0,0 +1,93 @@ +--- +subcategory: 'App Configuration' +layout: 'ibm' +page_title: 'IBM : App Configuration Snapshots' +description: |- + Get information about Snapshots +--- + +# ibm_app_config_segment +Retrieve information about an existing IBM Cloud App Configuration snapshots. You can then reference the fields of the data source in other resources within the same configuration using interpolation syntax. For more information, about App Configuration snapshot, see [App Configuration concepts](https://cloud.ibm.com//docs/app-configuration?topic=app-configuration-ac-overview). + +## Example usage + +```terraform +data "ibm_app_config_snapshots" "app_config_snapshots" { + guid = "guid" + sort = "sort" + collection_id = "collection_id" + environment_id = "environment_id" + limit = "limit" + offset = "offset" +} +``` + +## Argument reference + +Review the argument reference that you can specify for your data source. + +- `guid` - (Required, String) The GUID of the App Configuration service. Get it from the service instance credentials section of the dashboard. +- `collection_id` - (Optional, String) Filters the response based on the specified collection_id. +- `environment_id` - (Optional, String) Filters the response based on the specified environment_id. +- `limit` - (Optional, String) The number of records to retrieve. By default, the list operation return the first 10 records. +- `offset` - (Optional, String) The number of records to skip. By specifying `offset`, you retrieve a subset of items that starts with the `offset` value. + + +## Attribute reference + +In addition to all argument references list, you can access the following attribute references after your resource is created. + +- `limit` - (Integer) Number of records returned. +- `offset` - (Integer) Skipped number of records. +- `total_count` - (Integer) Total number of records + +- `first` - (List) The URL to navigate to the first page of records. + + Nested scheme for `first`: + - `href` - (String) The first `href` URL. + +- `last` - (List) The URL to navigate to the last page of records. + + Nested scheme for `last`: + - `href` - (String) The last `href` URL. + +- `previous` - (List) The URL to navigate to the previous list of records. + + Nested scheme for `previous`: + - `href` - (String) The previous `href` URL. + +- `next` - (List) The URL to navigate to the next list of records + + Nested scheme for `next`: + - `href` - (String) The next `href` URL. + + +- `git_config` - Array of git_configs. + + Nested scheme for `git_config`: + - `git_config_name` - (String) Git config name. Allowed special characters are dot ( . ), hyphen( - ), underscore ( _ ) only. + - `git_config_id` - (String) Git config id. Allowed special characters are dot ( . ), hyphen( - ), underscore ( _ ) only + - `git_url` - (String) Git url which will be used to connect to the github account. The url must be formed in this format, https://api.github.com/repos/{owner}/{repo_name} for the personal git account. + - `git_branch` - (String) Branch name to which you need to write or update the configuration. + - `git_file_path` - (String) Git file path, this is a path where your configuration file will be written. The path must contain the file name with `json` extension. + - `created_time` - (Timestamp) Creation time of the git config. + - `updated_time` - (Timestamp) Last modified time of the git config data. + - `last_sync_time` - (Timestamp) Latest time when the snapshot was synced to git. + - `href` - (String) Git config URL. + + - `collection` - (Object) Collection object will be returned containing attributes collection_id, collection_name. + + Nested scheme for `collection`: + - `collection_id` - (String) Collection id. + - `name` - (String) Name of the collection. + + - `environment` - (Object) Environment object will be returned containing attributes environment_id, environment_name, color_code. + + Nested scheme for `environment` : + - `environment_id` - (String) Environment Id. + - `environment_name` - (String) Environment name. + - `color_code` - (String) Color code to distinguish the environment. + + + + \ No newline at end of file diff --git a/website/docs/r/app_config_snapshot.html.markdown b/website/docs/r/app_config_snapshot.html.markdown new file mode 100644 index 00000000000..04e61c6a3a1 --- /dev/null +++ b/website/docs/r/app_config_snapshot.html.markdown @@ -0,0 +1,68 @@ +--- +subcategory: 'App Configuration' +layout: 'ibm' +page_title: 'IBM : App Configuration Snapshots' +description: |- + Manages snapshots. +--- + +# ibm_app_config_snapshots + +Provides a resource for `snapshot`. This allows snapshot to be created, updated and deleted. For more information, about App Configuration snapshots, see [segments](https://cloud.ibm.com/docs/app-configuration?topic=app-configuration-ac-snapshots). + +## Example usage + +```terraform +resource "ibm_app_config_snapshot" "app_config_snapshot" { + guid = "guid" + collection_id = "collection_id" + environment_id = "environment_id" + git_config_id = "git_config_id" + git_config_name = "git_config_name" + git_url = "git_url" + git_branch = "git_branch" + git_file_path = "git_file_path" + git_token = "git_token" +} +``` + +## Argument reference + +Review the argument reference that you can specify for your resource. + +- `guid` - (Required, String) The GUID of the App Configuration service. Fetch GUID from the service instance credentials section of the dashboard. +- `collection_id` - (Required, String) Collection ID +- `environment_id` - (Required, String) Environment Id +- `git_config_name` - (Required, String) Git config name. Allowed special characters are dot ( . ), hyphen( - ), underscore ( _ ) only. +- `git_config_id` - (Required, String) Git config id. Allowed special characters are dot ( . ), hyphen( - ), underscore ( _ ) only +- `git_url` - (Required, String) Git url which will be used to connect to the github account. The url must be formed in this format, https://api.github.com/repos/{owner}/{repo_name} for the personal git account. +- `git_branch` - (Required, String) Branch name to which you need to write or update the configuration. +- `git_file_path` - (Required, String) Git file path, this is a path where your configuration file will be written. The path must contain the file name with `json` extension. +- `git_token` - (Required, String) Git token, this needs to be provided with enough permission to write and update the file. + + +## Attribute reference + +In addition to all argument references list, you can access the following attribute references after your resource is created. + +- `created_time` - (Timestamp) Creation time of the segment. +- `updated_time` - (Timestamp) Last modified time of the segment data. +- `href` - (String) Git config URL. + + +## Import + +The `ibm_app_config_snapshot` resource can be imported by using `guid` of the App Configuration instance and `git_config_id`. Get the `guid` from the service instance credentials section of the dashboard. + +**Syntax** + +``` +terraform import ibm_app_config_snapshot.sample + +``` + +**Example** + +``` +terraform import ibm_app_config_snapshot.app_config_snapshot 272111153-c118-4116-8116-b811fbc31132/sample +``` \ No newline at end of file From 6209bebcca46e3964017353795023c8b92e7cb8c Mon Sep 17 00:00:00 2001 From: vhavalagi <94357468+vhavalagi@users.noreply.github.com> Date: Fri, 16 Sep 2022 20:36:38 +0530 Subject: [PATCH 2/4] Delete resource_ibm_app_config_snapshot_test.go --- .../resource_ibm_app_config_snapshot_test.go | 144 ------------------ 1 file changed, 144 deletions(-) delete mode 100644 ibm/service/appconfiguration/resource_ibm_app_config_snapshot_test.go diff --git a/ibm/service/appconfiguration/resource_ibm_app_config_snapshot_test.go b/ibm/service/appconfiguration/resource_ibm_app_config_snapshot_test.go deleted file mode 100644 index 661133de68c..00000000000 --- a/ibm/service/appconfiguration/resource_ibm_app_config_snapshot_test.go +++ /dev/null @@ -1,144 +0,0 @@ -package appconfiguration_test - -import ( - "fmt" - acc "github.com/IBM-Cloud/terraform-provider-ibm/ibm/acctest" - "github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" - "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" - "testing" - - "github.com/IBM/appconfiguration-go-admin-sdk/appconfigurationv1" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" -) - -func TestAccIbmIbmAppConfigSnapshotBasic(t *testing.T) { - var conf appconfigurationv1.SnapshotResponseGetApi - instanceName := fmt.Sprintf("tf_app_config_test_%d", acctest.RandIntRange(10, 100)) - git_config_name := fmt.Sprintf("tf_git_config_name_%d", acctest.RandIntRange(10, 100)) - git_config_id := fmt.Sprintf("tf_git_config_id_%d", acctest.RandIntRange(10, 100)) - git_url := fmt.Sprintf("tf_git_url_%d", acctest.RandIntRange(10, 100)) - git_branch := fmt.Sprintf("tf_git_branch_%d", acctest.RandIntRange(10, 100)) - git_file_path := fmt.Sprintf("tf_git_file_path_%d", acctest.RandIntRange(10, 100)) - git_token := fmt.Sprintf("tf_git_token_%d", acctest.RandIntRange(10, 100)) - collection_id := fmt.Sprintf("tf_collection_id_%d", acctest.RandIntRange(10, 100)) - git_config_nameUpdate := fmt.Sprintf("tf_git_config_name_%d", acctest.RandIntRange(10, 100)) - - resource.Test(t, resource.TestCase{ - PreCheck: func() { acc.TestAccPreCheck(t) }, - Providers: acc.TestAccProviders, - CheckDestroy: testAccCheckIbmAppConfigSnapshotDestroy, - Steps: []resource.TestStep{ - { - Config: testAccCheckIbmAppConfigSnapshotConfigBasic(instanceName, git_config_name, git_config_id, git_url, git_branch, git_file_path, - git_token, collection_id), - Check: resource.ComposeAggregateTestCheckFunc( - testAccCheckIbmAppConfigSnapshotExists("ibm_app_config_snapshot.ibm_app_config_snapshot_resource1", conf), - resource.TestCheckResourceAttrSet("ibm_app_config_snapshot.ibm_app_config_snapshot_resource1", "environment_id"), - resource.TestCheckResourceAttrSet("ibm_app_config_snapshot.ibm_app_config_snapshot_resource1", "name"), - resource.TestCheckResourceAttrSet("ibm_app_config_snapshot.ibm_app_config_snapshot_resource1", "property_id"), - resource.TestCheckResourceAttrSet("ibm_app_config_snapshot.ibm_app_config_snapshot_resource1", "type"), - resource.TestCheckResourceAttrSet("ibm_app_config_snapshot.ibm_app_config_snapshot_resource1", "description"), - resource.TestCheckResourceAttrSet("ibm_app_config_snapshot.ibm_app_config_snapshot_resource1", "tags"), - resource.TestCheckResourceAttrSet("ibm_app_config_snapshot.ibm_app_config_snapshot_resource1", "created_time"), - resource.TestCheckResourceAttrSet("ibm_app_config_snapshot.ibm_app_config_snapshot_resource1", "updated_time"), - resource.TestCheckResourceAttrSet("ibm_app_config_snapshot.ibm_app_config_snapshot_resource1", "href"), - resource.TestCheckResourceAttrSet("ibm_app_config_snapshot.ibm_app_config_snapshot_resource1", "segment_exists"), - ), - }, - { - Config: testAccCheckIbmAppConfigSnapshotConfigBasic(instanceName, git_config_nameUpdate, git_config_id, git_url, git_branch, git_file_path, - git_token, collection_id), - Check: resource.ComposeAggregateTestCheckFunc( - resource.TestCheckResourceAttr("ibm_app_config_snapshot.ibm_app_config_snapshot_resource1", "name", git_config_nameUpdate), - ), - }, - }, - }) - -} - -func testAccCheckIbmAppConfigSnapshotConfigBasic(instanceName, git_config_id, git_config_name, git_url, git_branch, git_file_path, - git_token, collection_id string) string { - return fmt.Sprintf(` - resource "ibm_resource_instance" "app_config_terraform_test476" { - name = "%s" - location = "us-south" - service = "apprapp" - plan = "lite" - } - resource "ibm_app_config_snapshot" "ibm_app_config_property_resource1" { - guid = ibm_resource_instance.app_config_terraform_test476.guid - git_config_id = "%s" - git_config_name = "%s" - git_url = "%s" - git_branch = "%s" - git_file_path = "%s" - git_token = "%s" - collection_id = "%s" - environment_id = "dev" - }`, instanceName, git_config_id, git_config_name, git_url, git_branch, git_file_path, - git_token, collection_id) -} - -func testAccCheckIbmAppConfigSnapshotExists(n string, obj appconfigurationv1.SnapshotResponseGetApi) resource.TestCheckFunc { - - return func(s *terraform.State) error { - rs, ok := s.RootModule().Resources[n] - if !ok { - return fmt.Errorf("Not found: %s", n) - } - - parts, err := flex.IdParts(rs.Primary.ID) - if err != nil { - return err - } - - appconfigClient, err := getAppConfigClient(acc.TestAccProvider.Meta(), parts[0]) - if err != nil { - return err - } - options := &appconfigurationv1.GetGitconfigOptions{} - - options.SetGitConfigID(parts[1]) - - snapshot, _, err := appconfigClient.GetGitconfig(options) - if err != nil { - return err - } - - obj = *snapshot - return nil - } -} - -func testAccCheckIbmAppConfigSnapshotDestroy(s *terraform.State) error { - for _, rs := range s.RootModule().Resources { - if rs.Type != "ibm_app_config_snapshot" { - continue - } - parts, err := flex.IdParts(rs.Primary.ID) - if err != nil { - return err - } - - appconfigClient, err := getAppConfigClient(acc.TestAccProvider.Meta(), parts[0]) - if err != nil { - return err - } - options := &appconfigurationv1.GetGitconfigOptions{} - - options.SetGitConfigID(parts[1]) - - // Try to find the key - _, response, err := appconfigClient.GetGitconfig(options) - - if err == nil { - return fmt.Errorf("app_config_snapshot still exists: %s", rs.Primary.ID) - } else if response.StatusCode != 404 { - return fmt.Errorf("Error checking for app_config_snapshot (%s) has been destroyed: %s", rs.Primary.ID, err) - } - } - - return nil -} From 871d0d717820839bb183c88fdd6259ed51fd2a52 Mon Sep 17 00:00:00 2001 From: vinayhavalagi Date: Fri, 16 Sep 2022 20:50:41 +0530 Subject: [PATCH 3/4] private end-point support --- ibm/conns/config.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/ibm/conns/config.go b/ibm/conns/config.go index 5c742997e27..600b32c7bb7 100644 --- a/ibm/conns/config.go +++ b/ibm/conns/config.go @@ -1770,12 +1770,18 @@ func (c *Config) ClientSession() (interface{}, error) { } // APP CONFIGURATION Service - if c.Visibility == "private" { - session.appConfigurationClientErr = fmt.Errorf("[ERROR] App Configuration Service API doesnot support private endpoints") + appconfigurl := ContructEndpoint(fmt.Sprintf("%s", c.Region), fmt.Sprintf("%s.apprapp.", cloudEndpoint)) + if c.Visibility == "private" || c.Visibility == "public-and-private" { + appconfigurl = ContructEndpoint(fmt.Sprintf("%s.private", c.Region), fmt.Sprintf("%s.apprapp", cloudEndpoint)) + } + if fileMap != nil && c.Visibility != "public-and-private" { + appconfigurl = fileFallBack(fileMap, c.Visibility, "IBMCLOUD_APP_CONFIG_ENDPOINT", c.Region, appconfigurl) } appConfigurationClientOptions := &appconfigurationv1.AppConfigurationV1Options{ + URL: EnvFallBack([]string{"IBMCLOUD_APP_CONFIG_ENDPOINT"}, appconfigurl), Authenticator: authenticator, } + appConfigClient, err := appconfigurationv1.NewAppConfigurationV1(appConfigurationClientOptions) if appConfigClient != nil { // Enable retries for API calls From 25678c0e1d4062700177c53e0fea6a600cf407f4 Mon Sep 17 00:00:00 2001 From: vinayhavalagi Date: Wed, 28 Sep 2022 20:52:02 +0530 Subject: [PATCH 4/4] Addressing the review comments --- .../data_source_ibm_app_config_snapshot.go | 8 ++++---- .../appconfiguration/resource_ibm_app_config_snapshot.go | 1 + 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/ibm/service/appconfiguration/data_source_ibm_app_config_snapshot.go b/ibm/service/appconfiguration/data_source_ibm_app_config_snapshot.go index bf1337e414a..bd03ec48747 100644 --- a/ibm/service/appconfiguration/data_source_ibm_app_config_snapshot.go +++ b/ibm/service/appconfiguration/data_source_ibm_app_config_snapshot.go @@ -23,22 +23,22 @@ func DataSourceIBMAppConfigSnapshot() *schema.Resource { }, "git_config_name": { Type: schema.TypeString, - Optional: true, + Computed: true, Description: "Git config name. Allowed special characters are dot ( . ), hyphen( - ), underscore ( _ ) only", }, "git_url": { Type: schema.TypeString, - Optional: true, + Computed: true, Description: "Git url which will be used to connect to the github account.", }, "git_branch": { Type: schema.TypeString, - Optional: true, + Computed: true, Description: "Branch name to which you need to write or update the configuration.", }, "git_file_path": { Type: schema.TypeString, - Optional: true, + Computed: true, Description: "Git file path, this is a path where your configuration file will be written.", }, "created_time": { diff --git a/ibm/service/appconfiguration/resource_ibm_app_config_snapshot.go b/ibm/service/appconfiguration/resource_ibm_app_config_snapshot.go index 13570003624..d5447b2b8e1 100644 --- a/ibm/service/appconfiguration/resource_ibm_app_config_snapshot.go +++ b/ibm/service/appconfiguration/resource_ibm_app_config_snapshot.go @@ -50,6 +50,7 @@ func ResourceIBMIbmAppConfigSnapshot() *schema.Resource { }, "git_token": { Type: schema.TypeString, + Sensitive: true, Required: true, Description: "Git token, this needs to be provided with enough permission to write and update the file.", },