-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add iec server data source and volume_attached block into iec server …
…resource (#1169) * add volume_attached block into iec server resource * add iec server data source and docs
- Loading branch information
1 parent
0d9db25
commit e1fe2e3
Showing
11 changed files
with
612 additions
and
55 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
--- | ||
subcategory: "Intelligent EdgeCloud (IEC)" | ||
--- | ||
|
||
# huaweicloud_iec_server | ||
|
||
Use this data source to get the details of a specified IEC server. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
variable "server_name" { } | ||
data "huaweicloud_iec_server" "demo" { | ||
name = var.server_name | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `name` - (Required, String) Specifies the IEC server name, which can be queried with a regular expression. | ||
|
||
* `status` - (Optional, String) Specifies the status of IEC server. | ||
|
||
## Attributes Reference | ||
|
||
In addition to all arguments above, the following attributes are exported: | ||
|
||
* `id` - The IEC server ID in UUID format. | ||
* `edgecloud_id` - The ID of the edgecloud service. | ||
* `edgecloud_name` - The Name of the edgecloud service. | ||
* `coverage_sites` - An array of site ID and operator for the IEC server. | ||
The object structure is documented below. | ||
* `flavor_id` - The flavor ID of the IEC server. | ||
* `flavor_name` - The flavor name of the IEC server. | ||
* `image_name` - The image name of the IEC server. | ||
* `vpc_id` - The ID of vpc for the IEC server. | ||
* `security_groups` - An array of one or more security group IDs to associate with the IEC server. | ||
* `nics` - An array of one or more networks to attach to the IEC server. | ||
The object structure is documented below. | ||
* `volume_attached` - An array of one or more disks to attach to the IEC server. | ||
The object structure is documented below. | ||
* `public_ip` - The EIP address that is associted to the IEC server. | ||
* `system_disk_id` - The system disk voume ID. | ||
|
||
The `coverage_sites` block supports: | ||
* `site_id` - The ID of IEC site. | ||
* `site_info` - The located information of the IEC site. It contains area, province and city. | ||
* `operator` - The operator of the IEC site. | ||
|
||
The `nics` block supports: | ||
* `port` - The port ID corresponding to the IP address on that network. | ||
* `mac` - The MAC address of the NIC on that network. | ||
* `address` - The IPv4 address of the server on that network. | ||
|
||
The `volume_attached` block supports: | ||
|
||
* `volume_id` - The volume ID on that attachment. | ||
* `boot_index` - The volume boot index on that attachment. | ||
* `size` - The volume size on that attachment. | ||
* `type` - The volume type on that attachment. | ||
* `device` - The device name in the IEC server. |
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,183 @@ | ||
package huaweicloud | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
"github.com/huaweicloud/golangsdk/openstack/iec/v1/servers" | ||
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config" | ||
) | ||
|
||
func dataSourceIECServer() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceIECServerRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"edgecloud_id": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
}, | ||
"status": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
}, | ||
|
||
"flavor_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"flavor_name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"image_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"image_name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"key_pair": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"user_data": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"vpc_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"security_groups": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
}, | ||
"edgecloud_name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"coverage_sites": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"site_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"site_info": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"operator": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
|
||
"nics": iecServerNicsSchema, | ||
"volume_attached": iecVolumeAttachedSchema, | ||
"public_ip": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"system_disk_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceIECServerRead(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*config.Config) | ||
iecClient, err := config.IECV1Client(GetRegion(d, config)) | ||
if err != nil { | ||
return fmt.Errorf("Error creating HuaweiCloud IEC client: %s", err) | ||
} | ||
|
||
listOpts := &servers.ListOpts{ | ||
Name: d.Get("name").(string), | ||
Status: d.Get("status").(string), | ||
EdgeCloudID: d.Get("edgecloud_id").(string), | ||
} | ||
|
||
log.Printf("[DEBUG] searching the IEC server by filter: %#v", listOpts) | ||
allServers, err := servers.List(iecClient, listOpts).Extract() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
total := len(allServers.Servers) | ||
if total < 1 { | ||
return fmt.Errorf("Your query returned no results. " + | ||
"Please change your search criteria and try again.") | ||
} | ||
if total > 1 { | ||
return fmt.Errorf("Your query returned more than one result. " + | ||
"Please try a more specific search criteria.") | ||
} | ||
|
||
server := allServers.Servers[0] | ||
log.Printf("[DEBUG] fetching the IEC server: %#v", server) | ||
|
||
d.SetId(server.ID) | ||
d.Set("name", server.Name) | ||
d.Set("status", server.Status) | ||
|
||
flavorInfo := server.Flavor | ||
d.Set("flavor_id", flavorInfo.ID) | ||
d.Set("flavor_name", flavorInfo.Name) | ||
d.Set("image_id", server.Image.ID) | ||
d.Set("image_name", server.Metadata.ImageName) | ||
|
||
if server.KeyName != "" { | ||
d.Set("key_pair", server.KeyName) | ||
} | ||
if server.UserData != "" { | ||
d.Set("user_data", server.UserData) | ||
} | ||
|
||
// set networking fields | ||
d.Set("vpc_id", server.Metadata.VpcID) | ||
secGrpIDs := make([]string, len(server.SecurityGroups)) | ||
for i, sg := range server.SecurityGroups { | ||
secGrpIDs[i] = sg.ID | ||
} | ||
d.Set("security_groups", secGrpIDs) | ||
|
||
allNics, eip := expandIecServerNics(&server) | ||
d.Set("nics", allNics) | ||
d.Set("public_ip", eip) | ||
|
||
// set volume fields | ||
allVolumes, sysDiskID := expandIecServerVolumeAttached(iecClient, &server) | ||
d.Set("volume_attached", allVolumes) | ||
d.Set("system_disk_id", sysDiskID) | ||
|
||
// set IEC fields | ||
location := server.Location | ||
siteInfo := fmt.Sprintf("%s/%s/%s/%s", location.Country, location.Area, location.Province, location.City) | ||
siteItem := map[string]interface{}{ | ||
"site_id": location.ID, | ||
"site_info": siteInfo, | ||
"operator": server.Operator.Name, | ||
} | ||
d.Set("coverage_sites", []map[string]interface{}{siteItem}) | ||
d.Set("edgecloud_id", server.EdgeCloudID) | ||
d.Set("edgecloud_name", server.EdgeCloudName) | ||
|
||
return nil | ||
} |
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,53 @@ | ||
package huaweicloud | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
) | ||
|
||
func TestAccIECServerDataSource_basic(t *testing.T) { | ||
rName := fmt.Sprintf("iec-%s", acctest.RandString(5)) | ||
resourceName := "data.huaweicloud_iec_server.server_1" | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckIecServerDestory, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccIecServer_basic(rName), | ||
}, | ||
{ | ||
Config: testAccIECServerDataSource_basic(rName), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr(resourceName, "name", "server-"+rName), | ||
resource.TestCheckResourceAttr(resourceName, "image_name", "Ubuntu 16.04 server 64bit"), | ||
resource.TestCheckResourceAttr(resourceName, "nics.#", "1"), | ||
resource.TestCheckResourceAttr(resourceName, "security_groups.#", "1"), | ||
resource.TestCheckResourceAttr(resourceName, "volume_attached.#", "1"), | ||
resource.TestCheckResourceAttr(resourceName, "volume_attached.0.boot_index", "0"), | ||
resource.TestCheckResourceAttr(resourceName, "volume_attached.0.type", "SAS"), | ||
resource.TestCheckResourceAttr(resourceName, "volume_attached.0.size", "40"), | ||
resource.TestCheckResourceAttr(resourceName, "status", "ACTIVE"), | ||
resource.TestCheckResourceAttrSet(resourceName, "system_disk_id"), | ||
resource.TestCheckResourceAttrSet(resourceName, "public_ip"), | ||
resource.TestCheckResourceAttrSet(resourceName, "coverage_sites.0.site_id"), | ||
resource.TestCheckResourceAttrSet(resourceName, "coverage_sites.0.site_info"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccIECServerDataSource_basic(rName string) string { | ||
return fmt.Sprintf(` | ||
%s | ||
data "huaweicloud_iec_server" "server_1" { | ||
name = huaweicloud_iec_server.server_test.name | ||
} | ||
`, testAccIecServer_basic(rName)) | ||
} |
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.