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

Add CSBS Backup and Policy service #42

Merged
merged 5 commits into from
Dec 3, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
64 changes: 64 additions & 0 deletions examples/csbs-backup-with-ecs/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
resource "huaweicloud_networking_network_v2" "terraform" {
name = "terraform"
admin_state_up = "true"
}

resource "huaweicloud_compute_secgroup_v2" "terraform" {
name = "terraform"
description = "Security group for the Terraform example instances"

rule {
from_port = 22
to_port = 22
ip_protocol = "tcp"
cidr = "0.0.0.0/0"
}

rule {
from_port = 80
to_port = 80
ip_protocol = "tcp"
cidr = "0.0.0.0/0"
}

rule {
from_port = -1
to_port = -1
ip_protocol = "icmp"
cidr = "0.0.0.0/0"
}
}

resource "huaweicloud_compute_instance_v2" "terraform" {
name = "terraform"
image_name = "${var.image}"
flavor_name = "${var.flavor}"
availability_zone = "${var.availability_zone}"
security_groups = ["${huaweicloud_compute_secgroup_v2.terraform.name}"]

network {
uuid = "${huaweicloud_networking_network_v2.terraform.id}"
}

}
resource "huaweicloud_csbs_backup_v1" "backup_v1" {
backup_name = "${var.project}-backup"
resource_id = "${huaweicloud_compute_instance_v2.terraform.id}"
resource_type = "OS::Nova::Server"
}

resource "huaweicloud_csbs_backup_policy_v1" "backup_policy_v1" {
name = "csbs-backup-policy"
resource {
id = "${huaweicloud_compute_instance_v2.terraform.id}"
type = "OS::Nova::Server"
name = "resource1"
}
scheduled_operation {
name ="mybackup"
enabled = true
operation_type ="backup"
max_backups = 2
trigger_pattern = "BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nRRULE:FREQ=WEEKLY;BYDAY=TH;BYHOUR=12;BYMINUTE=27\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n"
}
}
15 changes: 15 additions & 0 deletions examples/csbs-backup-with-ecs/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
variable "project" {
default = "terraform"
}

variable "image" {
default = "Ubuntu 14.04"
}

variable "flavor" {
default = "m1.small"
}

variable "availability_zone" {
default = "The first az of the az list"
}
7 changes: 7 additions & 0 deletions huaweicloud/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,13 @@ func (c *Config) autoscalingV1Client(region string) (*golangsdk.ServiceClient, e
})
}

func (c *Config) csbsV1Client(region string) (*golangsdk.ServiceClient, error) {
return huaweisdk.NewHwCSBSServiceV1(c.HwClient, golangsdk.EndpointOpts{
Region: c.determineRegion(region),
Availability: c.getHwEndpointType(),
})
}

func (c *Config) dmsV1Client(region string) (*golangsdk.ServiceClient, error) {
return huaweisdk.NewDMSServiceV1(c.HwClient, golangsdk.EndpointOpts{
Region: c.determineRegion(region),
Expand Down
179 changes: 179 additions & 0 deletions huaweicloud/data_source_huaweicloud_csbs_backup_policy_v1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
package huaweicloud

import (
"fmt"
"log"

"github.com/hashicorp/terraform/helper/schema"
"github.com/huaweicloud/golangsdk/openstack/csbs/v1/policies"
)

func dataSourceCSBSBackupPolicyV1() *schema.Resource {
return &schema.Resource{
Read: dataSourceCSBSBackupPolicyV1Read,

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,
},
"name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"description": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"provider_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"common": &schema.Schema{
Type: schema.TypeMap,
Computed: true,
},
"resource": &schema.Schema{
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"type": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
},
},
},
"scheduled_operation": &schema.Schema{
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"description": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"enabled": &schema.Schema{
Type: schema.TypeBool,
Computed: true,
},
"max_backups": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
},
"retention_duration_days": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
},
"permanent": &schema.Schema{
Type: schema.TypeBool,
Computed: true,
},
"trigger_pattern": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"operation_type": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"trigger_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"trigger_name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"trigger_type": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}

func dataSourceCSBSBackupPolicyV1Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
policyClient, err := config.csbsV1Client(GetRegion(d, config))

listOpts := policies.ListOpts{
ID: d.Get("id").(string),
Name: d.Get("name").(string),
Status: d.Get("status").(string),
}

refinedPolicies, err := policies.List(policyClient, listOpts)

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

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

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

backupPolicy := refinedPolicies[0]

log.Printf("[INFO] Retrieved backup policy %s using given filter", backupPolicy.ID)

d.SetId(backupPolicy.ID)

if err := d.Set("resource", flattenCSBSPolicyResources(backupPolicy)); err != nil {
return err
}

if err := d.Set("scheduled_operation", flattenCSBSScheduledOperations(backupPolicy)); err != nil {
return err
}

d.Set("name", backupPolicy.Name)
d.Set("id", backupPolicy.ID)
d.Set("common", backupPolicy.Parameters.Common)
d.Set("status", backupPolicy.Status)
d.Set("description", backupPolicy.Description)
d.Set("provider_id", backupPolicy.ProviderId)

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

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

import (
"fmt"
"testing"

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

func TestAccCSBSBackupPolicyV1DataSource_basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccCSBSBackupPolicyV1DataSource_basic,
Check: resource.ComposeTestCheckFunc(
testAccCheckCSBSBackupPolicyV1DataSourceID("data.huaweicloud_csbs_backup_policy_v1.csbs_policy"),
resource.TestCheckResourceAttr("data.huaweicloud_csbs_backup_policy_v1.csbs_policy", "name", "backup-policy"),
resource.TestCheckResourceAttr("data.huaweicloud_csbs_backup_policy_v1.csbs_policy", "status", "suspended"),
),
},
},
})
}

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

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

return nil
}
}

var testAccCSBSBackupPolicyV1DataSource_basic = fmt.Sprintf(`
resource "huaweicloud_compute_instance_v2" "instance_1" {
name = "instance_1"
image_id = "%s"
security_groups = ["default"]
availability_zone = "%s"
flavor_id = "%s"
metadata {
foo = "bar"
}
network {
uuid = "%s"
}
}
resource "huaweicloud_csbs_backup_policy_v1" "backup_policy_v1" {
name = "backup-policy"
resource {
id = "${huaweicloud_compute_instance_v2.instance_1.id}"
type = "OS::Nova::Server"
name = "resource4"
}
scheduled_operation {
name ="mybackup"
enabled = true
operation_type ="backup"
max_backups = "2"
trigger_pattern = "BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nRRULE:FREQ=WEEKLY;BYDAY=TH;BYHOUR=12;BYMINUTE=27\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n"
}
}

data "huaweicloud_csbs_backup_policy_v1" "csbs_policy" {
id = "${huaweicloud_csbs_backup_policy_v1.backup_policy_v1.id}"
}
`, OS_IMAGE_ID, OS_AVAILABILITY_ZONE, OS_FLAVOR_ID, OS_NETWORK_ID)
Loading