forked from rancher/norman
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request rancher#52 from rawmind0/logging_ds
Cluster and project logging data sources
- Loading branch information
Showing
15 changed files
with
444 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.