Skip to content
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

appconfig snapshot support and Private end-point support #4048

Merged
merged 5 commits into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions ibm/conns/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions ibm/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down
251 changes: 251 additions & 0 deletions ibm/service/appconfiguration/data_source_ibm_app_config_snapshot.go
Original file line number Diff line number Diff line change
@@ -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,
vhavalagi marked this conversation as resolved.
Show resolved Hide resolved
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
}
Loading