Skip to content

Commit

Permalink
Add dms server
Browse files Browse the repository at this point in the history
  • Loading branch information
root committed Sep 27, 2018
1 parent 0fd3d6c commit e65d22f
Show file tree
Hide file tree
Showing 33 changed files with 2,579 additions and 1 deletion.
7 changes: 7 additions & 0 deletions huaweicloud/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,3 +438,10 @@ func (c *Config) autoscalingV1Client(region string) (*golangsdk.ServiceClient, e
Availability: c.getHwEndpointType(),
})
}

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

import (
"fmt"
"log"

"github.com/hashicorp/terraform/helper/schema"
"github.com/huaweicloud/golangsdk/openstack/dms/v1/availablezones"
)

func dataSourceDmsAZV1() *schema.Resource {
return &schema.Resource{
Read: dataSourceDmsAZV1Read,

Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"code": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"port": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},
},
}
}

func dataSourceDmsAZV1Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
dmsV1Client, err := config.dmsV1Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud kms key client: %s", err)
}

v, err := availablezones.Get(dmsV1Client).Extract()
if err != nil {
return err
}

log.Printf("[DEBUG] Dms az : %+v", v)
var filteredAZs []availablezones.AvailableZone
if v.RegionID == GetRegion(d, config) {
AZs := v.AvailableZones
for _, newAZ := range AZs {
if newAZ.ResourceAvailability != "true" {
continue
}

name := d.Get("name").(string)
if name != "" && newAZ.Name != name {
continue
}

port := d.Get("port").(string)
if port != "" && newAZ.Port != port {
continue
}
filteredAZs = append(filteredAZs, newAZ)
}
}

if len(filteredAZs) < 1 {
return fmt.Errorf("Not found any available zones")
}

az := filteredAZs[0]
log.Printf("[DEBUG] Dms az : %+v", az)

d.SetId(az.ID)
d.Set("code", az.Code)
d.Set("name", az.Name)
d.Set("port", az.Port)
d.Set("resource_availability", az.ResourceAvailability)

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

import (
"fmt"
"testing"

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

func TestAccDmsAZV1DataSource_basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccDmsAZV1DataSource_basic,
Check: resource.ComposeTestCheckFunc(
testAccCheckDmsAZV1DataSourceID("data.huaweicloud_dms_az_v1.az1"),
resource.TestCheckResourceAttr(
"data.huaweicloud_dms_az_v1.az1", "name", "可用区1"),
resource.TestCheckResourceAttr(
"data.huaweicloud_dms_az_v1.az1", "port", "8002"),
resource.TestCheckResourceAttr(
"data.huaweicloud_dms_az_v1.az1", "code", "cn-north-1a"),
),
},
},
})
}

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

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

return nil
}
}

var testAccDmsAZV1DataSource_basic = fmt.Sprintf(`
data "huaweicloud_dms_az_v1" "az1" {
name = "可用区1"
port = "8002"
code = "cn-north-1a"
}
`)
87 changes: 87 additions & 0 deletions huaweicloud/data_source_huaweicloud_dms_maintainwindow_v1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package huaweicloud

import (
"fmt"
"log"
"strconv"

"github.com/hashicorp/terraform/helper/schema"
"github.com/huaweicloud/golangsdk/openstack/dms/v1/maintainwindows"
)

func dataSourceDmsMaintainWindowV1() *schema.Resource {
return &schema.Resource{
Read: dataSourceDmsMaintainWindowV1Read,

Schema: map[string]*schema.Schema{
"seq": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
"begin": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"end": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"default": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Computed: true,
},
},
}
}

func dataSourceDmsMaintainWindowV1Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
dmsV1Client, err := config.dmsV1Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud kms key client: %s", err)
}

v, err := maintainwindows.Get(dmsV1Client).Extract()
if err != nil {
return err
}

maintainWindows := v.MaintainWindows
var filteredMVs []maintainwindows.MaintainWindow
for _, mv := range maintainWindows {
seq := d.Get("seq").(int)
if seq != 0 && mv.ID != seq {
continue
}

begin := d.Get("begin").(string)
if begin != "" && mv.Begin != begin {
continue
}
end := d.Get("end").(string)
if end != "" && mv.End != end {
continue
}

df := d.Get("default").(bool)
if mv.Default != df {
continue
}
filteredMVs = append(filteredMVs, mv)
}
if len(filteredMVs) < 1 {
return fmt.Errorf("Your query returned no results. Please change your filters and try again.")
}
mw := filteredMVs[0]
d.SetId(strconv.Itoa(mw.ID))
d.Set("begin", mw.Begin)
d.Set("end", mw.End)
d.Set("default", mw.Default)
log.Printf("[DEBUG] Dms MaintainWindow : %+v", mw)

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

import (
"fmt"
"testing"

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

func TestAccDmsMaintainWindowV1DataSource_basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccDmsMaintainWindowV1DataSource_basic,
Check: resource.ComposeTestCheckFunc(
testAccCheckDmsMaintainWindowV1DataSourceID("data.huaweicloud_dms_maintainwindow_v1.maintainwindow1"),
resource.TestCheckResourceAttr(
"data.huaweicloud_dms_maintainwindow_v1.maintainwindow1", "seq", "1"),
resource.TestCheckResourceAttr(
"data.huaweicloud_dms_maintainwindow_v1.maintainwindow1", "begin", "22"),
),
},
},
})
}

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

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

return nil
}
}

var testAccDmsMaintainWindowV1DataSource_basic = fmt.Sprintf(`
data "huaweicloud_dms_maintainwindow_v1" "maintainwindow1" {
seq = 1
}
`)
Loading

0 comments on commit e65d22f

Please sign in to comment.