Skip to content

Commit

Permalink
Merge pull request rancher#145 from rawmind0/monitoring
Browse files Browse the repository at this point in the history
Added argument to define monitoring config for rancher2_cluster and rancher2_project
  • Loading branch information
rawmind0 authored Oct 8, 2019
2 parents efce729 + b0951ee commit 59aaeff
Show file tree
Hide file tree
Showing 17 changed files with 355 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ FEATURES:

ENHANCEMENTS:

* Added `monitoring_input` argument to define monitoring config for `rancher2_cluster` and `rancher2_project`
* Improved capitalization/spelling/grammar/etc in docs

BUG FIXES:
Expand Down
9 changes: 9 additions & 0 deletions rancher2/data_source_rancher2_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ func dataSourceRancher2Cluster() *schema.Resource {
Schema: clusterAuthEndpoint(),
},
},
"cluster_monitoring_input": &schema.Schema{
Type: schema.TypeList,
MaxItems: 1,
Computed: true,
Description: "Cluster monitoring configuration",
Elem: &schema.Resource{
Schema: monitoringInputFields(),
},
},
"cluster_registration_token": &schema.Schema{
Type: schema.TypeList,
MaxItems: 1,
Expand Down
11 changes: 1 addition & 10 deletions rancher2/import_rancher2_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,7 @@ import (
)

func resourceRancher2ProjectImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
client, err := meta.(*Config).ManagementClient()
if err != nil {
return []*schema.ResourceData{}, err
}
project, err := client.Project.ByID(d.Id())
if err != nil {
return []*schema.ResourceData{}, err
}

err = flattenProject(d, project)
err := resourceRancher2ProjectRead(d, meta)
if err != nil {
return []*schema.ResourceData{}, err
}
Expand Down
47 changes: 45 additions & 2 deletions rancher2/resource_rancher2_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,20 @@ func resourceRancher2ClusterCreate(d *schema.ResourceData, meta interface{}) err
return fmt.Errorf("[ERROR] waiting for cluster (%s) to be created: %s", newCluster.ID, waitErr)
}

monitoringInput := expandMonitoringInput(d.Get("cluster_monitoring_input").([]interface{}))
if newCluster.EnableClusterMonitoring && monitoringInput != nil {
clusterResource := &norman.Resource{
ID: newCluster.ID,
Type: newCluster.Type,
Links: newCluster.Links,
Actions: newCluster.Actions,
}
err = client.APIBaseClient.Action(managementClient.ClusterType, "editMonitoring", clusterResource, monitoringInput, nil)
if err != nil {
return err
}
}

d.SetId(newCluster.ID)

return resourceRancher2ClusterRead(d, meta)
Expand Down Expand Up @@ -118,7 +132,20 @@ func resourceRancher2ClusterRead(d *schema.ResourceData, meta interface{}) error
return err
}

err = flattenCluster(d, cluster, clusterRegistrationToken, kubeConfig, defaultProjectID, systemProjectID)
monitoringInput := &managementClient.MonitoringInput{}
if cluster.EnableClusterMonitoring {
monitoringOutput := &managementClient.MonitoringOutput{}
err = client.APIBaseClient.Action(managementClient.ClusterType, "viewMonitoring", clusterResource, nil, monitoringOutput)
if err != nil {
return err
}

if monitoringOutput != nil && len(monitoringOutput.Answers) > 0 {
monitoringInput.Answers = monitoringOutput.Answers
}
}

err = flattenCluster(d, cluster, clusterRegistrationToken, kubeConfig, defaultProjectID, systemProjectID, monitoringInput)
if err != nil {
return err
}
Expand Down Expand Up @@ -179,7 +206,7 @@ func resourceRancher2ClusterUpdate(d *schema.ResourceData, meta interface{}) err
update["rancherKubernetesEngineConfig"] = rkeConfig
}

newCluster := &CloudCredential{}
newCluster := &Cluster{}
err = client.APIBaseClient.Update(managementClient.ClusterType, cluster, update, newCluster)
if err != nil {
return err
Expand All @@ -198,6 +225,22 @@ func resourceRancher2ClusterUpdate(d *schema.ResourceData, meta interface{}) err
return fmt.Errorf("[ERROR] waiting for cluster (%s) to be updated: %s", newCluster.ID, waitErr)
}

if d.HasChange("enable_cluster_monitoring") || d.HasChange("cluster_monitoring_input") {
monitoringInput := expandMonitoringInput(d.Get("cluster_monitoring_input").([]interface{}))
if newCluster.EnableClusterMonitoring && monitoringInput != nil {
clusterResource := &norman.Resource{
ID: newCluster.ID,
Type: newCluster.Type,
Links: newCluster.Links,
Actions: newCluster.Actions,
}
err = client.APIBaseClient.Action(managementClient.ClusterType, "editMonitoring", clusterResource, monitoringInput, nil)
if err != nil {
return err
}
}
}

d.SetId(newCluster.ID)

return resourceRancher2ClusterRead(d, meta)
Expand Down
34 changes: 32 additions & 2 deletions rancher2/resource_rancher2_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,15 @@ func resourceRancher2ProjectCreate(d *schema.ResourceData, meta interface{}) err
"[ERROR] waiting for project (%s) to be created: %s", newProject.ID, waitErr)
}

err = flattenProject(d, newProject)
monitoringInput := expandMonitoringInput(d.Get("project_monitoring_input").([]interface{}))
if newProject.EnableProjectMonitoring && monitoringInput != nil {
err = client.Project.ActionEditMonitoring(newProject, monitoringInput)
if err != nil {
return err
}
}

err = flattenProject(d, newProject, monitoringInput)
if err != nil {
return err
}
Expand All @@ -111,7 +119,19 @@ func resourceRancher2ProjectRead(d *schema.ResourceData, meta interface{}) error
return err
}

err = flattenProject(d, project)
monitoringInput := &managementClient.MonitoringInput{}
if project.EnableProjectMonitoring {
monitoringOutput, err := client.Project.ActionViewMonitoring(project)
if err != nil {
return err
}

if monitoringOutput != nil && len(monitoringOutput.Answers) > 0 {
monitoringInput.Answers = monitoringOutput.Answers
}
}

err = flattenProject(d, project, monitoringInput)
if err != nil {
return err
}
Expand Down Expand Up @@ -163,6 +183,16 @@ func resourceRancher2ProjectUpdate(d *schema.ResourceData, meta interface{}) err
"[ERROR] waiting for project (%s) to be updated: %s", newProject.ID, waitErr)
}

if d.HasChange("enable_project_monitoring") || d.HasChange("project_monitoring_input") {
monitoringInput := expandMonitoringInput(d.Get("project_monitoring_input").([]interface{}))
if newProject.EnableProjectMonitoring && monitoringInput != nil {
err = client.Project.ActionEditMonitoring(newProject, monitoringInput)
if err != nil {
return err
}
}
}

return resourceRancher2ProjectRead(d, meta)
}

Expand Down
10 changes: 10 additions & 0 deletions rancher2/schema_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,16 @@ func clusterFields() map[string]*schema.Schema {
Schema: clusterAuthEndpoint(),
},
},
"cluster_monitoring_input": &schema.Schema{
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Computed: true,
Description: "Cluster monitoring configuration",
Elem: &schema.Resource{
Schema: monitoringInputFields(),
},
},
"cluster_registration_token": &schema.Schema{
Type: schema.TypeList,
MaxItems: 1,
Expand Down
19 changes: 19 additions & 0 deletions rancher2/schema_monitoring_input.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package rancher2

import (
"github.com/hashicorp/terraform/helper/schema"
)

//Schemas

func monitoringInputFields() map[string]*schema.Schema {
s := map[string]*schema.Schema{
"answers": &schema.Schema{
Type: schema.TypeMap,
Optional: true,
Description: "Answers for monitor input",
},
}

return s
}
10 changes: 10 additions & 0 deletions rancher2/schema_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,16 @@ func projectFields() map[string]*schema.Schema {
Type: schema.TypeString,
Optional: true,
},
"project_monitoring_input": &schema.Schema{
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Computed: true,
Description: "Cluster monitoring configuration",
Elem: &schema.Resource{
Schema: monitoringInputFields(),
},
},
"resource_quota": {
Type: schema.TypeList,
MaxItems: 1,
Expand Down
7 changes: 6 additions & 1 deletion rancher2/structure_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func flattenClusterAuthEndpoint(in *managementClient.LocalClusterAuthEndpoint) [
return []interface{}{obj}
}

func flattenCluster(d *schema.ResourceData, in *Cluster, clusterRegToken *managementClient.ClusterRegistrationToken, kubeConfig *managementClient.GenerateKubeConfigOutput, defaultProjectID, systemProjectID string) error {
func flattenCluster(d *schema.ResourceData, in *Cluster, clusterRegToken *managementClient.ClusterRegistrationToken, kubeConfig *managementClient.GenerateKubeConfigOutput, defaultProjectID, systemProjectID string, monitoringInput *managementClient.MonitoringInput) error {
if in == nil {
return fmt.Errorf("[ERROR] flattening cluster: Input cluster is nil")
}
Expand Down Expand Up @@ -95,6 +95,11 @@ func flattenCluster(d *schema.ResourceData, in *Cluster, clusterRegToken *manage
return err
}

err = d.Set("cluster_monitoring_input", flattenMonitoringInput(monitoringInput))
if err != nil {
return err
}

d.Set("kube_config", kubeConfig.Config)
d.Set("default_project_id", defaultProjectID)
d.Set("system_project_id", systemProjectID)
Expand Down
2 changes: 1 addition & 1 deletion rancher2/structure_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ func TestFlattenCluster(t *testing.T) {
for _, tc := range cases {
output := schema.TestResourceDataRaw(t, clusterFields(), map[string]interface{}{})
tc.InputToken.ID = "id"
err := flattenCluster(output, tc.Input, tc.InputToken, tc.InputKube, tc.ExpectedOutput["default_project_id"].(string), tc.ExpectedOutput["system_project_id"].(string))
err := flattenCluster(output, tc.Input, tc.InputToken, tc.InputKube, tc.ExpectedOutput["default_project_id"].(string), tc.ExpectedOutput["system_project_id"].(string), nil)
if err != nil {
t.Fatalf("[ERROR] on flattener: %#v", err)
}
Expand Down
37 changes: 37 additions & 0 deletions rancher2/structure_monitoring_input.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package rancher2

import (
managementClient "github.com/rancher/types/client/management/v3"
)

// Flatteners

func flattenMonitoringInput(in *managementClient.MonitoringInput) []interface{} {
obj := map[string]interface{}{}

if in == nil {
return []interface{}{}
}

if len(in.Answers) > 0 {
obj["answers"] = toMapInterface(in.Answers)
}

return []interface{}{obj}
}

// Expanders

func expandMonitoringInput(p []interface{}) *managementClient.MonitoringInput {
obj := &managementClient.MonitoringInput{}
if len(p) == 0 || p[0] == nil {
return nil
}
in := p[0].(map[string]interface{})

if v, ok := in["answers"].(map[string]interface{}); ok && len(v) > 0 {
obj.Answers = toMapString(v)
}

return obj
}
72 changes: 72 additions & 0 deletions rancher2/structure_monitoring_input_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package rancher2

import (
"reflect"
"testing"

managementClient "github.com/rancher/types/client/management/v3"
)

var (
testMonitoringInputConf *managementClient.MonitoringInput
testMonitoringInputInterface []interface{}
)

func init() {
testMonitoringInputConf = &managementClient.MonitoringInput{
Answers: map[string]string{
"answer_one": "one",
"answer_two": "two",
},
}
testMonitoringInputInterface = []interface{}{
map[string]interface{}{
"answers": map[string]interface{}{
"answer_one": "one",
"answer_two": "two",
},
},
}
}

func TestFlattenMonitoringInput(t *testing.T) {

cases := []struct {
Input *managementClient.MonitoringInput
ExpectedOutput []interface{}
}{
{
testMonitoringInputConf,
testMonitoringInputInterface,
},
}

for _, tc := range cases {
output := flattenMonitoringInput(tc.Input)
if !reflect.DeepEqual(output, tc.ExpectedOutput) {
t.Fatalf("Unexpected output from flattener.\nExpected: %#v\nGiven: %#v",
tc.ExpectedOutput, output)
}
}
}

func TestExpandMonitoringInput(t *testing.T) {

cases := []struct {
Input []interface{}
ExpectedOutput *managementClient.MonitoringInput
}{
{
testMonitoringInputInterface,
testMonitoringInputConf,
},
}

for _, tc := range cases {
output := expandMonitoringInput(tc.Input)
if !reflect.DeepEqual(output, tc.ExpectedOutput) {
t.Fatalf("Unexpected output from expander.\nExpected: %#v\nGiven: %#v",
tc.ExpectedOutput, output)
}
}
}
9 changes: 7 additions & 2 deletions rancher2/structure_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func flattenProjectResourceQuota(pQuota *managementClient.ProjectResourceQuota,
return []interface{}{obj}
}

func flattenProject(d *schema.ResourceData, in *managementClient.Project) error {
func flattenProject(d *schema.ResourceData, in *managementClient.Project, monitoringInput *managementClient.MonitoringInput) error {
if in == nil {
return nil
}
Expand Down Expand Up @@ -138,7 +138,12 @@ func flattenProject(d *schema.ResourceData, in *managementClient.Project) error
}
}

err := d.Set("annotations", toMapInterface(in.Annotations))
err := d.Set("project_monitoring_input", flattenMonitoringInput(monitoringInput))
if err != nil {
return err
}

err = d.Set("annotations", toMapInterface(in.Annotations))
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion rancher2/structure_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func TestFlattenProject(t *testing.T) {

for _, tc := range cases {
output := schema.TestResourceDataRaw(t, projectFields(), map[string]interface{}{})
err := flattenProject(output, tc.Input)
err := flattenProject(output, tc.Input, nil)
if err != nil {
t.Fatalf("[ERROR] on flattener: %#v", err)
}
Expand Down
Loading

0 comments on commit 59aaeff

Please sign in to comment.