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 CCE service #19

Merged
merged 17 commits into from
Nov 16, 2018
Merged
Show file tree
Hide file tree
Changes from 10 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
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
## 1.1.1 (Unreleased)
## 1.1.2 (Unreleased)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why adding a new release stanza 1.1.2? You should not manually changed the v1.1.1 below to released which should be triggered by hashicorp release bot.


FEATURES:

* **New Data Source:** `huaweicloud_cce_cluster_v3` [GH-19]
* **New Data Source:** `huaweicloud_cce_node_v3` [GH-19]
* **New Resource:** `huaweicloud_cce_cluster_v3` [GH-19]
* **New Resource:** `huaweicloud_cce_node_v3` [GH-19]

## 1.1.1 (July 24, 2018)
FEATURES:

* **New Data Source:** `huaweicloud_vpc_v1` [GH-14]
* **New Data Source:** `huaweicloud_vpc_peering_connection_v2` [GH-14]
* **New Data Source:** `huaweicloud_vpc_route_v2` [GH-14]
Expand Down
7 changes: 7 additions & 0 deletions huaweicloud/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,13 @@ func (c *Config) networkingV2Client(region string) (*gophercloud.ServiceClient,
})
}

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

func (c *Config) objectStorageV1Client(region string) (*gophercloud.ServiceClient, error) {
// If Swift Authentication is being used, return a swauth client.
if c.Swauth {
Expand Down
158 changes: 158 additions & 0 deletions huaweicloud/data_source_huaweicloud_cce_cluster_v3.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
package huaweicloud

import (
"fmt"
"log"

"github.com/hashicorp/terraform/helper/schema"
"github.com/huaweicloud/golangsdk/openstack/cce/v3/clusters"
)

func dataSourceCCEClusterV3() *schema.Resource {
return &schema.Resource{
Read: dataSourceCCEClusterV3Read,

Schema: map[string]*schema.Schema{
"region": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"flavor": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"description": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"cluster_version": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"cluster_type": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"billing_mode": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
},
"vpc_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"subnet_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"highway_subnet_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"container_network_type": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"container_network_cidr": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"status": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"endpoints": &schema.Schema{
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"url": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"type": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
}},
},
},
}
}

func dataSourceCCEClusterV3Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
cceClient, err := config.cceV3Client(GetRegion(d, config))

if err != nil {
return fmt.Errorf("Unable to create HuaweiCloud CCE client : %s", err)
}

listOpts := clusters.ListOpts{
ID: d.Get("id").(string),
Name: d.Get("name").(string),
Type: d.Get("cluster_type").(string),
Phase: d.Get("status").(string),
VpcID: d.Get("vpc_id").(string),
}

refinedClusters, err := clusters.List(cceClient, listOpts)
log.Printf("[DEBUG] Value of allClusters: %#v", refinedClusters)
if err != nil {
return fmt.Errorf("Unable to retrieve clusters: %s", err)
}

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

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

Cluster := refinedClusters[0]

log.Printf("[DEBUG] Retrieved Clusters using given filter %s: %+v", Cluster.Metadata.Id, Cluster)
var v []map[string]interface{}
for _, endpoint := range Cluster.Status.Endpoints {

mapping := map[string]interface{}{
"url": endpoint.Url,
"type": endpoint.Type,
}
v = append(v, mapping)
}

d.SetId(Cluster.Metadata.Id)

d.Set("name", Cluster.Metadata.Name)
d.Set("flavor", Cluster.Spec.Flavor)
d.Set("description", Cluster.Spec.Description)
d.Set("cluster_version", Cluster.Spec.Version)
d.Set("cluster_type", Cluster.Spec.Type)
d.Set("billing_mode", Cluster.Spec.BillingMode)
d.Set("vpc_id", Cluster.Spec.HostNetwork.VpcId)
d.Set("subnet_id", Cluster.Spec.HostNetwork.SubnetId)
d.Set("highway_subnet_id", Cluster.Spec.HostNetwork.HighwaySubnet)
d.Set("container_network_cidr", Cluster.Spec.ContainerNetwork.Cidr)
d.Set("container_network_type", Cluster.Spec.ContainerNetwork.Mode)
d.Set("status", Cluster.Status.Phase)
if err := d.Set("endpoints", v); err != nil {
return err
}
d.Set("region", GetRegion(d, config))

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

import (
"fmt"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"testing"
)

func TestAccCCEClusterV3DataSource_basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheckCCE(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccCCEClusterV3DataSource_cluster,
},
resource.TestStep{
Config: testAccCCEClusterV3DataSource_basic,
Check: resource.ComposeTestCheckFunc(
testAccCheckCCEClusterV3DataSourceID("data.huaweicloud_cce_cluster_v3.clusters"),
resource.TestCheckResourceAttr("data.huaweicloud_cce_cluster_v3.clusters", "name", "huaweicloud-cce"),
resource.TestCheckResourceAttr("data.huaweicloud_cce_cluster_v3.clusters", "status", "Available"),
resource.TestCheckResourceAttr("data.huaweicloud_cce_cluster_v3.clusters", "cluster_type", "VirtualMachine"),
),
},
},
})
}

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

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

return nil
}
}

var testAccCCEClusterV3DataSource_cluster = fmt.Sprintf(`
resource "huaweicloud_cce_cluster_v3" "cluster_1" {
name = "huaweicloud-cce"
cluster_type = "VirtualMachine"
flavor = "cce.s1.small"
cluster_version = "v1.7.3-r10"
vpc_id = "%s"
subnet_id = "%s"
container_network_type = "overlay_l2"
}`, OS_VPC_ID, OS_SUBNET_ID)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all such OS_SUBNET_ID should be changed to OS_NETWORK_ID which we already added.


var testAccCCEClusterV3DataSource_basic = fmt.Sprintf(`
%s
data "huaweicloud_cce_cluster_v3" "clusters" {
name = "${huaweicloud_cce_cluster_v3.cluster_1.name}"
}
`, testAccCCEClusterV3DataSource_cluster)
Loading