Skip to content

Commit

Permalink
Add CTS Tracker service (#46)
Browse files Browse the repository at this point in the history
* add cts tracker resource service

* add cts tracker resource docs
  • Loading branch information
ninja-vlogs authored and niuzhenguo committed Nov 30, 2018
1 parent 1aa5ac2 commit e41827c
Show file tree
Hide file tree
Showing 17 changed files with 1,126 additions and 5 deletions.
18 changes: 18 additions & 0 deletions examples/cts_tracker.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
resource "huaweicloud_s3_bucket" "bucket" {
bucket = "tf-test-bucket"
acl = "public-read"
}
resource "huaweicloud_smn_topic_v2" "topic_1" {
name = "topic_check"
display_name = "The display name of topic_1"
}

resource "huaweicloud_cts_tracker_v1" "tracker_v1" {
bucket_name = "${huaweicloud_s3_bucket.bucket.bucket}"
file_prefix_name = "yO8Q"
is_support_smn = true
topic_id = "${huaweicloud_smn_topic_v2.topic_1.id}"
is_send_all_key_operation = false
operations = ["delete","create","login"]
need_notify_user_list = ["user1"]
}
7 changes: 7 additions & 0 deletions huaweicloud/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,3 +459,10 @@ func (c *Config) vbsV2Client(region string) (*golangsdk.ServiceClient, error) {
Availability: c.getHwEndpointType(),
})
}

func (c *Config) ctsV1Client(region string) (*golangsdk.ServiceClient, error) {
return huaweisdk.NewCTSService(c.HwClient, golangsdk.EndpointOpts{
Region: c.determineRegion(region),
Availability: c.getHwEndpointType(),
})
}
116 changes: 116 additions & 0 deletions huaweicloud/data_source_huaweicloud_cts_tracker_v1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package huaweicloud

import (
"fmt"
"log"

"github.com/hashicorp/terraform/helper/schema"
"github.com/huaweicloud/golangsdk/openstack/cts/v1/tracker"
)

func dataSourceCTSTrackerV1() *schema.Resource {
return &schema.Resource{
Read: dataSourceCTSTrackerV1Read,

Schema: map[string]*schema.Schema{
"region": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"status": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"bucket_name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"file_prefix_name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"tracker_name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"is_support_smn": &schema.Schema{
Type: schema.TypeBool,
Computed: true,
},
"topic_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"operations": &schema.Schema{
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
"is_send_all_key_operation": &schema.Schema{
Type: schema.TypeBool,
Computed: true,
},
"need_notify_user_list": &schema.Schema{
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
},
}
}

func dataSourceCTSTrackerV1Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
trackerClient, err := config.ctsV1Client(GetRegion(d, config))

listOpts := tracker.ListOpts{
TrackerName: d.Get("tracker_name").(string),
BucketName: d.Get("bucket_name").(string),
FilePrefixName: d.Get("file_prefix_name").(string),
Status: d.Get("status").(string),
}

refinedTrackers, err := tracker.List(trackerClient, listOpts)

if err != nil {
return fmt.Errorf("Unable to retrieve cts tracker: %s", err)
}

if len(refinedTrackers) < 1 {
return fmt.Errorf("Your query returned no results. " +
"Please change your search criteria and try again.")
}

if len(refinedTrackers) > 1 {
return fmt.Errorf("Your query returned more than one result." +
" Please try a more specific search criteria")
}

trackers := refinedTrackers[0]

log.Printf("[INFO] Retrieved cts tracker %s using given filter", trackers.TrackerName)

d.SetId(trackers.TrackerName)

d.Set("tracker_name", trackers.TrackerName)
d.Set("bucket_name", trackers.BucketName)
d.Set("file_prefix_name", trackers.FilePrefixName)
d.Set("status", trackers.Status)
d.Set("is_support_smn", trackers.SimpleMessageNotification.IsSupportSMN)
d.Set("topic_id", trackers.SimpleMessageNotification.TopicID)
d.Set("is_send_all_key_operation", trackers.SimpleMessageNotification.IsSendAllKeyOperation)
d.Set("operations", trackers.SimpleMessageNotification.Operations)
d.Set("need_notify_user_list", trackers.SimpleMessageNotification.NeedNotifyUserList)

d.Set("region", GetRegion(d, config))

return nil
}
67 changes: 67 additions & 0 deletions huaweicloud/data_source_huaweicloud_cts_tracker_v1_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package huaweicloud

import (
"fmt"
"testing"

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

func TestAccCTSTrackerV1DataSource_basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccCTSTrackerV1DataSource_basic,
Check: resource.ComposeTestCheckFunc(
testAccCheckCTSTrackerV1DataSourceID("data.huaweicloud_cts_tracker_v1.tracker_v1"),
resource.TestCheckResourceAttr("data.huaweicloud_cts_tracker_v1.tracker_v1", "bucket_name", "tf-test-bucket"),
resource.TestCheckResourceAttr("data.huaweicloud_cts_tracker_v1.tracker_v1", "status", "enabled"),
),
},
},
})
}

func testAccCheckCTSTrackerV1DataSourceID(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Can't find cts tracker data source: %s ", n)
}

if rs.Primary.ID == "" {
return fmt.Errorf("tracker data source not set ")
}

return nil
}
}

const testAccCTSTrackerV1DataSource_basic = `
resource "huaweicloud_s3_bucket" "bucket" {
bucket = "tf-test-bucket"
acl = "public-read"
force_destroy = true
}
resource "huaweicloud_smn_topic_v2" "topic_1" {
name = "tf-test-topic"
display_name = "The display name of tf-test-topic"
}
resource "huaweicloud_cts_tracker_v1" "tracker_v1" {
bucket_name = "${huaweicloud_s3_bucket.bucket.bucket}"
file_prefix_name = "yO8Q"
is_support_smn = true
topic_id = "${huaweicloud_smn_topic_v2.topic_1.id}"
is_send_all_key_operation = false
operations = ["login"]
need_notify_user_list = ["user1"]
}
data "huaweicloud_cts_tracker_v1" "tracker_v1" {
tracker_name = "${huaweicloud_cts_tracker_v1.tracker_v1.id}"
}
`
28 changes: 28 additions & 0 deletions huaweicloud/import_huaweicloud_cts_tracker_v1_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package huaweicloud

import (
"testing"

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

func TestAccCTSTrackerV1_importBasic(t *testing.T) {
resourceName := "huaweicloud_cts_tracker_v1.tracker_v1"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckCTSTrackerV1Destroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccCTSTrackerV1_basic,
},

resource.TestStep{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
2 changes: 2 additions & 0 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ func Provider() terraform.ResourceProvider {
"huaweicloud_dms_maintainwindow_v1": dataSourceDmsMaintainWindowV1(),
"huaweicloud_vbs_backup_policy_v2": dataSourceVBSBackupPolicyV2(),
"huaweicloud_vbs_backup_v2": dataSourceVBSBackupV2(),
"huaweicloud_cts_tracker_v1": dataSourceCTSTrackerV1(),
},

ResourcesMap: map[string]*schema.Resource{
Expand Down Expand Up @@ -281,6 +282,7 @@ func Provider() terraform.ResourceProvider {
"huaweicloud_as_policy_v1": resourceASPolicy(),
"huaweicloud_vbs_backup_policy_v2": resourceVBSBackupPolicyV2(),
"huaweicloud_vbs_backup_v2": resourceVBSBackupV2(),
"huaweicloud_cts_tracker_v1": resourceCTSTrackerV1(),
},

ConfigureFunc: configureProvider,
Expand Down
Loading

0 comments on commit e41827c

Please sign in to comment.