Skip to content

Commit

Permalink
Merge pull request #11 from manhtu1997/iam-vserver
Browse files Browse the repository at this point in the history
update function update security group
  • Loading branch information
manhtu1997 authored Dec 14, 2022
2 parents 75b88b2 + 54d99e3 commit 99f8f1f
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 14 deletions.
3 changes: 0 additions & 3 deletions docs/resources/vserver_server.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ The following arguments are supported:
- **action** (String, Optional) Action with server. It can be `stop`, `start` and `reboot`.
- **attach_floating** (Boolean, Optional) Is Server attach a floating IP.
- **expire_password** (Boolean, Optional) Skip change password: false, else: true
- **is_poc** (Boolean, Optional) POC wallet.
- **os_licence** (Boolean, Optional) Licence of OS.
- **root_disk_encryption_type** (String, Optional) Type encryption of boot volume.
- **security_group** (List of String, Optional) ID of the SecGroups.
Expand Down Expand Up @@ -81,8 +80,6 @@ In addition to all arguments above, the following attributes are exported:
- **subnet_uuid** (String)
- **type** (String)
- **os_info** (String) Info OS.
- **owner_email** (String) Email owner of Server.
- **share** (Boolean) Is Server Shared?
- **ssh_key_name** (String) Name of SSH Key.


5 changes: 1 addition & 4 deletions docs/resources/vserver_volume.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,12 @@ The following arguments are supported:
- **size** (Number, Required) Size of Volume.
- **volume_type_id** (String, Required) ID of Volume's Type
- **encryption_type** (String, Optional) Type encryption of volume.
- **is_poc** (Boolean, Optional) POC wallet.

## Attributes Reference

In addition to all arguments above, the following attributes are exported:

- **id** (String) The ID of this Volume.
- **owner_email** (String) Email owner of this Volume.
- **share** (Boolean) Is volume shared?
- **bootable** (Boolean, Required) Bootbale of Volume
- **bootable** (Boolean, Required) Bootable of Volume


23 changes: 17 additions & 6 deletions resource/vserver/resource_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func ResourceServer() *schema.Resource {
Elem: &schema.Schema{
Type: schema.TypeString,
},
Optional: true,
Required: true,
},
"source_type": {
Type: schema.TypeString,
Expand Down Expand Up @@ -191,11 +191,6 @@ func resourceServerStateRefreshFunc(cli *client.Client, serverID string, project
}
func resourceServerCreate(d *schema.ResourceData, m interface{}) error {
projectID := d.Get("project_id").(string)
securityGroupInterface := d.Get("security_group").([]interface{})
var securityGroup []string
for _, s := range securityGroupInterface {
securityGroup = append(securityGroup, s.(string))
}
if _, ok := d.GetOk("root_disk_size"); !ok {
return fmt.Errorf(`The argument "root_disk_size" is required, but no definition was found.`)
}
Expand All @@ -208,6 +203,14 @@ func resourceServerCreate(d *schema.ResourceData, m interface{}) error {
return fmt.Errorf(`The argument "user data" is set, the argument "user data base64 encode" is required, but no definition was found.`)
}
}
securityGroupInterface := d.Get("security_group").([]interface{})
var securityGroup []string
for _, s := range securityGroupInterface {
securityGroup = append(securityGroup, s.(string))
}
if len(securityGroup) == 0 {
return fmt.Errorf(`The argument "security_group" must not be empty.`)
}
server := vserver.CreateServerRequest{
AttachFloating: d.Get("attach_floating").(bool),
EncryptionVolume: d.Get("encryption_volume").(bool),
Expand Down Expand Up @@ -317,6 +320,11 @@ func resourceServerRead(d *schema.ResourceData, m interface{}) error {
externalInterfaces = append(externalInterfaces, externalInterfaceMap)
}
d.Set("external_interfaces", externalInterfaces)
var securityGroups []string
for _, secGroup := range server.SecGroups {
securityGroups = append(securityGroups, secGroup.Uuid)
}
d.Set("security_group", securityGroups)
return nil
}

Expand Down Expand Up @@ -504,6 +512,9 @@ func resourceServerUpdateSecgroup(d *schema.ResourceData, m interface{}) error {
for _, s := range securityGroupInterface {
securityGroup = append(securityGroup, s.(string))
}
if len(securityGroup) == 0 {
return fmt.Errorf(`The argument "security_group" must not be empty.`)
}
serverChangeSecGroup := vserver.ChangeSecGroupRequest{
ServerId: d.Id(),
SecurityGroup: securityGroup,
Expand Down
6 changes: 6 additions & 0 deletions resource/vserver/resource_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ func ResourceVolume() *schema.Resource {
Type: schema.TypeBool,
Computed: true,
},
"multi_attach": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
},
},
}
}
Expand All @@ -86,6 +91,7 @@ func resourceVolumeCreate(d *schema.ResourceData, m interface{}) error {
Name: d.Get("name").(string),
Size: int32(d.Get("size").(int)),
VolumeTypeId: d.Get("volume_type_id").(string),
MultiAttach: d.Get("multi_attach").(bool),
}
cli := m.(*client.Client)
resp, httpResponse, err := cli.VserverClient.VolumeRestControllerApi.CreateVolumeUsingPOST1(context.TODO(), a, projectID)
Expand Down
6 changes: 5 additions & 1 deletion resource/vserver/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,9 @@ func CheckErrorResponse(httpResponse *http.Response) bool {

func GetResponseBody(httpResponse *http.Response) string {
localVarBody, _ := io.ReadAll(httpResponse.Body)
return fmt.Sprint("Status Code: ", httpResponse.StatusCode, " , ", string(localVarBody))
responseMessage := string(localVarBody)
if httpResponse.StatusCode == 403 {
responseMessage = "You don't have permission to do this action"
}
return fmt.Sprint("Status Code: ", httpResponse.StatusCode, ", ", responseMessage)
}

0 comments on commit 99f8f1f

Please sign in to comment.