Skip to content

Commit

Permalink
Merge pull request rancher#52 from rawmind0/logging_ds
Browse files Browse the repository at this point in the history
Cluster and project logging data sources
  • Loading branch information
rawmind0 authored Aug 2, 2019
2 parents c187879 + 77fa791 commit 3c93fc1
Show file tree
Hide file tree
Showing 15 changed files with 444 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ FEATURES:
* **New Data Source:** `rancher2_cloud_credential`
* **New Data Source:** `rancher2_cluster`
* **New Data Source:** `rancher2_cluster_driver`
* **New Data Source:** `rancher2_cluster_logging`
* **New Data Source:** `rancher2_cluster_role_template_binding`
* **New Data Source:** `rancher2_etcd_backup`
* **New Data Source:** `rancher2_global_role_binding`
* **New Data Source:** `rancher2_namespace`
* **New Data Source:** `rancher2_node_driver`
* **New Data Source:** `rancher2_node_pool`
* **New Data Source:** `rancher2_project_logging`
* **New Data Source:** `rancher2_project_role_template_binding`
* **New Data Source:** `rancher2_user`
* **New Resource:** `rancher2_global_role_binding`
Expand Down
117 changes: 117 additions & 0 deletions rancher2/data_source_rancher2_cluster_logging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package rancher2

import (
"fmt"

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

func dataSourceRancher2ClusterLogging() *schema.Resource {
return &schema.Resource{
Read: dataSourceRancher2ClusterLoggingRead,

Schema: map[string]*schema.Schema{
"cluster_id": {
Type: schema.TypeString,
Required: true,
},
"kind": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"elasticsearch_config": &schema.Schema{
Type: schema.TypeList,
MaxItems: 1,
Computed: true,
Elem: &schema.Resource{
Schema: loggingElasticsearchConfigFields(),
},
},
"fluentd_config": &schema.Schema{
Type: schema.TypeList,
MaxItems: 1,
Computed: true,
Elem: &schema.Resource{
Schema: loggingFluentdConfigFields(),
},
},
"kafka_config": &schema.Schema{
Type: schema.TypeList,
MaxItems: 1,
Computed: true,
Elem: &schema.Resource{
Schema: loggingKafkaConfigFields(),
},
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"namespace_id": {
Type: schema.TypeString,
Computed: true,
},
"output_flush_interval": {
Type: schema.TypeInt,
Computed: true,
},
"output_tags": &schema.Schema{
Type: schema.TypeMap,
Computed: true,
},
"splunk_config": &schema.Schema{
Type: schema.TypeList,
MaxItems: 1,
Computed: true,
Elem: &schema.Resource{
Schema: loggingSplunkConfigFields(),
},
},
"syslog_config": &schema.Schema{
Type: schema.TypeList,
MaxItems: 1,
Computed: true,
Elem: &schema.Resource{
Schema: loggingSyslogConfigFields(),
},
},
"annotations": &schema.Schema{
Type: schema.TypeMap,
Computed: true,
},
"labels": &schema.Schema{
Type: schema.TypeMap,
Computed: true,
},
},
}
}

func dataSourceRancher2ClusterLoggingRead(d *schema.ResourceData, meta interface{}) error {
client, err := meta.(*Config).ManagementClient()
if err != nil {
return err
}

clusterID := d.Get("cluster_id").(string)

filters := map[string]interface{}{
"clusterId": clusterID,
}
listOpts := NewListOpts(filters)

clusterLoggings, err := client.ClusterLogging.List(listOpts)
if err != nil {
return err
}

count := len(clusterLoggings.Data)
if count <= 0 {
return fmt.Errorf("[ERROR] cluster logging on cluster ID \"%s\" not found", clusterID)
}
if count > 1 {
return fmt.Errorf("[ERROR] found %d cluster logging on cluster ID \"%s\"", count, clusterID)
}

return flattenClusterLogging(d, &clusterLoggings.Data[0])
}
52 changes: 52 additions & 0 deletions rancher2/data_source_rancher2_cluster_logging_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package rancher2

import (
"testing"

"github.com/hashicorp/terraform/helper/resource"
)

const (
testAccRancher2ClusterLoggingDataSourceType = "rancher2_cluster_logging"
)

var (
testAccCheckRancher2ClusterLoggingDataSourceConfig string
)

func init() {
testAccCheckRancher2ClusterLoggingDataSourceConfig = `
resource "rancher2_cluster_logging" "foo" {
name = "foo"
cluster_id = "` + testAccRancher2ClusterID + `"
kind = "syslog"
syslog_config {
endpoint = "192.168.1.1:514"
protocol = "udp"
severity = "notice"
ssl_verify = false
}
}
data "` + testAccRancher2ClusterLoggingDataSourceType + `" "foo" {
cluster_id = "${rancher2_cluster_logging.foo.cluster_id}"
}
`
}

func TestAccRancher2ClusterLoggingDataSource(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckRancher2ClusterLoggingDataSourceConfig,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data."+testAccRancher2ClusterLoggingDataSourceType+".foo", "name", "foo"),
resource.TestCheckResourceAttr("data."+testAccRancher2ClusterLoggingDataSourceType+".foo", "kind", "syslog"),
resource.TestCheckResourceAttr("data."+testAccRancher2ClusterLoggingDataSourceType+".foo", "syslog_config.0.endpoint", "192.168.1.1:514"),
),
},
},
})
}
117 changes: 117 additions & 0 deletions rancher2/data_source_rancher2_project_logging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package rancher2

import (
"fmt"

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

func dataSourceRancher2ProjectLogging() *schema.Resource {
return &schema.Resource{
Read: dataSourceRancher2ProjectLoggingRead,

Schema: map[string]*schema.Schema{
"project_id": {
Type: schema.TypeString,
Required: true,
},
"kind": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"elasticsearch_config": &schema.Schema{
Type: schema.TypeList,
MaxItems: 1,
Computed: true,
Elem: &schema.Resource{
Schema: loggingElasticsearchConfigFields(),
},
},
"fluentd_config": &schema.Schema{
Type: schema.TypeList,
MaxItems: 1,
Computed: true,
Elem: &schema.Resource{
Schema: loggingFluentdConfigFields(),
},
},
"kafka_config": &schema.Schema{
Type: schema.TypeList,
MaxItems: 1,
Computed: true,
Elem: &schema.Resource{
Schema: loggingKafkaConfigFields(),
},
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"namespace_id": {
Type: schema.TypeString,
Computed: true,
},
"output_flush_interval": {
Type: schema.TypeInt,
Computed: true,
},
"output_tags": &schema.Schema{
Type: schema.TypeMap,
Computed: true,
},
"splunk_config": &schema.Schema{
Type: schema.TypeList,
MaxItems: 1,
Computed: true,
Elem: &schema.Resource{
Schema: loggingSplunkConfigFields(),
},
},
"syslog_config": &schema.Schema{
Type: schema.TypeList,
MaxItems: 1,
Computed: true,
Elem: &schema.Resource{
Schema: loggingSyslogConfigFields(),
},
},
"annotations": &schema.Schema{
Type: schema.TypeMap,
Computed: true,
},
"labels": &schema.Schema{
Type: schema.TypeMap,
Computed: true,
},
},
}
}

func dataSourceRancher2ProjectLoggingRead(d *schema.ResourceData, meta interface{}) error {
client, err := meta.(*Config).ManagementClient()
if err != nil {
return err
}

projectID := d.Get("project_id").(string)

filters := map[string]interface{}{
"projectId": projectID,
}
listOpts := NewListOpts(filters)

projectLoggings, err := client.ProjectLogging.List(listOpts)
if err != nil {
return err
}

count := len(projectLoggings.Data)
if count <= 0 {
return fmt.Errorf("[ERROR] project logging on project ID \"%s\" not found", projectID)
}
if count > 1 {
return fmt.Errorf("[ERROR] found %d project logging on project ID \"%s\"", count, projectID)
}

return flattenProjectLogging(d, &projectLoggings.Data[0])
}
58 changes: 58 additions & 0 deletions rancher2/data_source_rancher2_project_logging_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package rancher2

import (
"testing"

"github.com/hashicorp/terraform/helper/resource"
)

const (
testAccRancher2ProjectLoggingDataSourceType = "rancher2_project_logging"
)

var (
testAccCheckRancher2ProjectLoggingDataSourceConfig string
)

func init() {
testAccCheckRancher2ProjectLoggingDataSourceConfig = `
resource "rancher2_project" "foo" {
name = "foo"
cluster_id = "` + testAccRancher2ClusterID + `"
description = "Terraform Project Logging acceptance test"
}
resource "rancher2_project_logging" "foo" {
name = "foo"
project_id = "${rancher2_project.foo.id}"
kind = "syslog"
syslog_config {
endpoint = "192.168.1.1:514"
protocol = "udp"
severity = "notice"
ssl_verify = false
}
}
data "` + testAccRancher2ProjectLoggingDataSourceType + `" "foo" {
project_id = "${rancher2_project_logging.foo.project_id}"
}
`
}

func TestAccRancher2ProjectLoggingDataSource(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckRancher2ProjectLoggingDataSourceConfig,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data."+testAccRancher2ProjectLoggingDataSourceType+".foo", "name", "foo"),
resource.TestCheckResourceAttr("data."+testAccRancher2ProjectLoggingDataSourceType+".foo", "kind", "syslog"),
resource.TestCheckResourceAttr("data."+testAccRancher2ProjectLoggingDataSourceType+".foo", "syslog_config.0.endpoint", "192.168.1.1:514"),
),
},
},
})
}
2 changes: 2 additions & 0 deletions rancher2/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,15 @@ func Provider() terraform.ResourceProvider {
"rancher2_cloud_credential": dataSourceRancher2CloudCredential(),
"rancher2_cluster": dataSourceRancher2Cluster(),
"rancher2_cluster_driver": dataSourceRancher2ClusterDriver(),
"rancher2_cluster_logging": dataSourceRancher2ClusterLogging(),
"rancher2_cluster_role_template_binding": dataSourceRancher2ClusterRoleTemplateBinding(),
"rancher2_etcd_backup": dataSourceRancher2EtcdBackup(),
"rancher2_global_role_binding": dataSourceRancher2GlobalRoleBinding(),
"rancher2_namespace": dataSourceRancher2Namespace(),
"rancher2_node_driver": dataSourceRancher2NodeDriver(),
"rancher2_node_pool": dataSourceRancher2NodePool(),
"rancher2_project": dataSourceRancher2Project(),
"rancher2_project_logging": dataSourceRancher2ProjectLogging(),
"rancher2_project_role_template_binding": dataSourceRancher2ProjectRoleTemplateBinding(),
"rancher2_setting": dataSourceRancher2Setting(),
"rancher2_user": dataSourceRancher2User(),
Expand Down
Loading

0 comments on commit 3c93fc1

Please sign in to comment.