0
0
mirror of https://github.com/bpg/terraform-provider-proxmox.git synced 2025-07-05 13:33:58 +00:00

Latest work

This commit is contained in:
Dan Petersen 2019-12-08 22:03:54 +01:00
parent b6655b2a4d
commit 97ae8ba5ed
13 changed files with 103 additions and 37 deletions

View File

@ -28,7 +28,7 @@ func dataSourceVirtualEnvironmentGroup() *schema.Resource {
Required: true, Required: true,
}, },
mkDataSourceVirtualEnvironmentGroupMembers: &schema.Schema{ mkDataSourceVirtualEnvironmentGroupMembers: &schema.Schema{
Type: schema.TypeList, Type: schema.TypeSet,
Description: "The group members", Description: "The group members",
Computed: true, Computed: true,
Elem: &schema.Schema{Type: schema.TypeString}, Elem: &schema.Schema{Type: schema.TypeString},
@ -55,7 +55,12 @@ func dataSourceVirtualEnvironmentGroupRead(d *schema.ResourceData, m interface{}
d.SetId(groupID) d.SetId(groupID)
if accessGroup.Comment != nil {
d.Set(mkDataSourceVirtualEnvironmentGroupComment, accessGroup.Comment) d.Set(mkDataSourceVirtualEnvironmentGroupComment, accessGroup.Comment)
} else {
d.Set(mkDataSourceVirtualEnvironmentGroupComment, "")
}
d.Set(mkDataSourceVirtualEnvironmentGroupMembers, accessGroup.Members) d.Set(mkDataSourceVirtualEnvironmentGroupMembers, accessGroup.Members)
return nil return nil

View File

@ -51,7 +51,12 @@ func dataSourceVirtualEnvironmentGroupsRead(d *schema.ResourceData, m interface{
groupIDs := make([]interface{}, len(list)) groupIDs := make([]interface{}, len(list))
for i, v := range list { for i, v := range list {
if v.Comment != nil {
comments[i] = v.Comment comments[i] = v.Comment
} else {
comments[i] = ""
}
groupIDs[i] = v.ID groupIDs[i] = v.ID
} }

View File

@ -22,7 +22,7 @@ func dataSourceVirtualEnvironmentRole() *schema.Resource {
Required: true, Required: true,
}, },
mkDataSourceVirtualEnvironmentRolePrivileges: &schema.Schema{ mkDataSourceVirtualEnvironmentRolePrivileges: &schema.Schema{
Type: schema.TypeList, Type: schema.TypeSet,
Description: "The role privileges", Description: "The role privileges",
Computed: true, Computed: true,
Elem: &schema.Schema{Type: schema.TypeString}, Elem: &schema.Schema{Type: schema.TypeString},
@ -47,8 +47,17 @@ func dataSourceVirtualEnvironmentRoleRead(d *schema.ResourceData, m interface{})
return err return err
} }
privileges := schema.NewSet(schema.HashString, make([]interface{}, 0))
if *accessRole != nil {
for _, v := range *accessRole {
privileges.Add(v)
}
}
d.SetId(roleID) d.SetId(roleID)
d.Set(mkDataSourceVirtualEnvironmentRolePrivileges, *accessRole)
d.Set(mkDataSourceVirtualEnvironmentRolePrivileges, privileges)
return nil return nil
} }

View File

@ -22,7 +22,7 @@ func dataSourceVirtualEnvironmentRoles() *schema.Resource {
Description: "The role privileges", Description: "The role privileges",
Computed: true, Computed: true,
Elem: &schema.Schema{ Elem: &schema.Schema{
Type: schema.TypeList, Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString}, Elem: &schema.Schema{Type: schema.TypeString},
}, },
}, },
@ -62,9 +62,25 @@ func dataSourceVirtualEnvironmentRolesRead(d *schema.ResourceData, m interface{}
special := make([]interface{}, len(list)) special := make([]interface{}, len(list))
for i, v := range list { for i, v := range list {
privileges[i] = v.Privileges if v.Privileges != nil {
p := schema.NewSet(schema.HashString, make([]interface{}, 0))
for _, v := range *v.Privileges {
p.Add(v)
}
privileges[i] = p
} else {
privileges[i] = make(map[string]interface{})
}
roleIDs[i] = v.ID roleIDs[i] = v.ID
if v.Special != nil {
special[i] = v.Special special[i] = v.Special
} else {
special[i] = false
}
} }
d.SetId("access_roles") d.SetId("access_roles")

View File

@ -1,4 +1,6 @@
data "proxmox_virtual_environment_groups" "example" {} data "proxmox_virtual_environment_groups" "example" {
depends_on = ["proxmox_virtual_environment_group.example"]
}
output "data_proxmox_virtual_environment_groups_example" { output "data_proxmox_virtual_environment_groups_example" {
value = "${map( value = "${map(

View File

@ -1,4 +1,6 @@
data "proxmox_virtual_environment_roles" "example" {} data "proxmox_virtual_environment_roles" "example" {
depends_on = ["proxmox_virtual_environment_role.example"]
}
output "data_proxmox_virtual_environment_roles_example_privileges" { output "data_proxmox_virtual_environment_roles_example_privileges" {
value = "${data.proxmox_virtual_environment_roles.example.privileges}" value = "${data.proxmox_virtual_environment_roles.example.privileges}"

View File

@ -1,5 +1,6 @@
resource "proxmox_virtual_environment_role" "example" { resource "proxmox_virtual_environment_role" "example" {
privileges = [ privileges = [
"VM.Console",
"VM.Monitor", "VM.Monitor",
] ]
role_id = "terraform-provider-proxmox-example" role_id = "terraform-provider-proxmox-example"

View File

@ -94,6 +94,8 @@ func (c *VirtualEnvironmentClient) DoRequest(method, path string, requestBody in
encodedValues := v.Encode() encodedValues := v.Encode()
urlEncodedRequestBody = bytes.NewBufferString(encodedValues) urlEncodedRequestBody = bytes.NewBufferString(encodedValues)
log.Printf("[DEBUG] Added request body to HTTP %s request (path: %s) - Body: %s", method, path, encodedValues)
} }
req, err := http.NewRequest(method, fmt.Sprintf("%s/%s/%s", c.Endpoint, basePathJSONAPI, path), urlEncodedRequestBody) req, err := http.NewRequest(method, fmt.Sprintf("%s/%s/%s", c.Endpoint, basePathJSONAPI, path), urlEncodedRequestBody)
@ -141,6 +143,8 @@ func (c *VirtualEnvironmentClient) DoRequest(method, path string, requestBody in
func (c *VirtualEnvironmentClient) ValidateResponseCode(res *http.Response) error { func (c *VirtualEnvironmentClient) ValidateResponseCode(res *http.Response) error {
if res.StatusCode < 200 || res.StatusCode >= 300 { if res.StatusCode < 200 || res.StatusCode >= 300 {
switch res.StatusCode { switch res.StatusCode {
case 400:
return fmt.Errorf("Received a HTTP %d response - This is most likely caused by a bug in the code, so please create a new issue on https://github.com/danitso/terraform-provider-proxmox/issues", res.StatusCode)
case 401: case 401:
return fmt.Errorf("Received a HTTP %d response - Please verify that the specified credentials are valid", res.StatusCode) return fmt.Errorf("Received a HTTP %d response - Please verify that the specified credentials are valid", res.StatusCode)
case 403: case 403:

View File

@ -29,10 +29,10 @@ type VirtualEnvironmentAuthenticationResponseCapabilities struct {
// VirtualEnvironmentAuthenticationResponseData contains the data from an authentication response. // VirtualEnvironmentAuthenticationResponseData contains the data from an authentication response.
type VirtualEnvironmentAuthenticationResponseData struct { type VirtualEnvironmentAuthenticationResponseData struct {
ClusterName string `json:"clustername,omitempty"` ClusterName *string `json:"clustername,omitempty"`
CSRFPreventionToken string `json:"CSRFPreventionToken"` CSRFPreventionToken *string `json:"CSRFPreventionToken,omitempty"`
Capabilities *VirtualEnvironmentAuthenticationResponseCapabilities `json:"cap,omitempty"` Capabilities *VirtualEnvironmentAuthenticationResponseCapabilities `json:"cap,omitempty"`
Ticket string `json:"ticket"` Ticket *string `json:"ticket,omitempty"`
Username string `json:"username"` Username string `json:"username"`
} }
@ -74,11 +74,11 @@ func (c *VirtualEnvironmentClient) Authenticate(reset bool) error {
return errors.New("The server did not include a data object in the authentication response") return errors.New("The server did not include a data object in the authentication response")
} }
if resBody.Data.CSRFPreventionToken == "" { if resBody.Data.CSRFPreventionToken == nil {
return errors.New("The server did not include a CSRF prevention token in the authentication response") return errors.New("The server did not include a CSRF prevention token in the authentication response")
} }
if resBody.Data.Ticket == "" { if resBody.Data.Ticket == nil {
return errors.New("The server did not include a ticket in the authentication response") return errors.New("The server did not include a ticket in the authentication response")
} }
@ -101,11 +101,11 @@ func (c *VirtualEnvironmentClient) AuthenticateRequest(req *http.Request) error
req.AddCookie(&http.Cookie{ req.AddCookie(&http.Cookie{
Name: "PVEAuthCookie", Name: "PVEAuthCookie",
Value: c.authenticationData.Ticket, Value: *c.authenticationData.Ticket,
}) })
if req.Method != "GET" { if req.Method != "GET" {
req.Header.Add("CSRFPreventionToken", c.authenticationData.CSRFPreventionToken) req.Header.Add("CSRFPreventionToken", *c.authenticationData.CSRFPreventionToken)
} }
return nil return nil

View File

@ -13,7 +13,7 @@ import (
// VirtualEnvironmentGroupCreateRequestBody contains the data for an access group create request. // VirtualEnvironmentGroupCreateRequestBody contains the data for an access group create request.
type VirtualEnvironmentGroupCreateRequestBody struct { type VirtualEnvironmentGroupCreateRequestBody struct {
Comment string `json:"comment" url:"comment"` Comment *string `json:"comment,omitempty" url:"comment,omitempty"`
ID string `json:"groupid" url:"groupid"` ID string `json:"groupid" url:"groupid"`
} }
@ -24,7 +24,7 @@ type VirtualEnvironmentGroupGetResponseBody struct {
// VirtualEnvironmentGroupGetResponseData contains the data from an access group get response. // VirtualEnvironmentGroupGetResponseData contains the data from an access group get response.
type VirtualEnvironmentGroupGetResponseData struct { type VirtualEnvironmentGroupGetResponseData struct {
Comment string `json:"comment"` Comment *string `json:"comment,omitempty"`
Members []string `json:"members"` Members []string `json:"members"`
} }
@ -35,13 +35,13 @@ type VirtualEnvironmentGroupListResponseBody struct {
// VirtualEnvironmentGroupListResponseData contains the data from an access group list response. // VirtualEnvironmentGroupListResponseData contains the data from an access group list response.
type VirtualEnvironmentGroupListResponseData struct { type VirtualEnvironmentGroupListResponseData struct {
Comment string `json:"comment"` Comment *string `json:"comment,omitempty"`
ID string `json:"groupid"` ID string `json:"groupid"`
} }
// VirtualEnvironmentGroupUpdateRequestBody contains the data for an access group update request. // VirtualEnvironmentGroupUpdateRequestBody contains the data for an access group update request.
type VirtualEnvironmentGroupUpdateRequestBody struct { type VirtualEnvironmentGroupUpdateRequestBody struct {
Comment string `json:"comment" url:"comment"` Comment *string `json:"comment,omitempty" url:"comment,omitempty"`
} }
// CreateGroup creates an access group. // CreateGroup creates an access group.

View File

@ -30,8 +30,8 @@ type VirtualEnvironmentRoleListResponseBody struct {
// VirtualEnvironmentRoleListResponseData contains the data from an access group list response. // VirtualEnvironmentRoleListResponseData contains the data from an access group list response.
type VirtualEnvironmentRoleListResponseData struct { type VirtualEnvironmentRoleListResponseData struct {
ID string `json:"roleid"` ID string `json:"roleid"`
Privileges *CustomPrivileges `json:"privs"` Privileges *CustomPrivileges `json:"privs,omitempty"`
Special CustomBool `json:"special"` Special *CustomBool `json:"special,omitempty"`
} }
// VirtualEnvironmentRoleUpdateRequestBody contains the data for an access group update request. // VirtualEnvironmentRoleUpdateRequestBody contains the data for an access group update request.
@ -84,6 +84,12 @@ func (c *VirtualEnvironmentClient) ListRoles() ([]*VirtualEnvironmentRoleListRes
return resBody.Data[i].ID < resBody.Data[j].ID return resBody.Data[i].ID < resBody.Data[j].ID
}) })
for i := range resBody.Data {
if resBody.Data[i].Privileges != nil {
sort.Strings(*resBody.Data[i].Privileges)
}
}
return resBody.Data, nil return resBody.Data, nil
} }

View File

@ -32,7 +32,7 @@ func resourceVirtualEnvironmentGroup() *schema.Resource {
Required: true, Required: true,
}, },
mkResourceVirtualEnvironmentGroupMembers: &schema.Schema{ mkResourceVirtualEnvironmentGroupMembers: &schema.Schema{
Type: schema.TypeList, Type: schema.TypeSet,
Description: "The group members", Description: "The group members",
Computed: true, Computed: true,
Elem: &schema.Schema{Type: schema.TypeString}, Elem: &schema.Schema{Type: schema.TypeString},
@ -53,9 +53,11 @@ func resourceVirtualEnvironmentGroupCreate(d *schema.ResourceData, m interface{}
return err return err
} }
comment := d.Get(mkResourceVirtualEnvironmentGroupComment).(string)
groupID := d.Get(mkResourceVirtualEnvironmentGroupID).(string) groupID := d.Get(mkResourceVirtualEnvironmentGroupID).(string)
body := &proxmox.VirtualEnvironmentGroupCreateRequestBody{ body := &proxmox.VirtualEnvironmentGroupCreateRequestBody{
Comment: d.Get(mkResourceVirtualEnvironmentGroupComment).(string), Comment: &comment,
ID: groupID, ID: groupID,
} }
@ -93,7 +95,12 @@ func resourceVirtualEnvironmentGroupRead(d *schema.ResourceData, m interface{})
d.SetId(groupID) d.SetId(groupID)
if accessGroup.Comment != nil {
d.Set(mkResourceVirtualEnvironmentGroupComment, accessGroup.Comment) d.Set(mkResourceVirtualEnvironmentGroupComment, accessGroup.Comment)
} else {
d.Set(mkResourceVirtualEnvironmentGroupComment, "")
}
d.Set(mkResourceVirtualEnvironmentGroupMembers, accessGroup.Members) d.Set(mkResourceVirtualEnvironmentGroupMembers, accessGroup.Members)
return nil return nil
@ -107,11 +114,13 @@ func resourceVirtualEnvironmentGroupUpdate(d *schema.ResourceData, m interface{}
return err return err
} }
comment := d.Get(mkResourceVirtualEnvironmentGroupComment).(string)
groupID := d.Id()
body := &proxmox.VirtualEnvironmentGroupUpdateRequestBody{ body := &proxmox.VirtualEnvironmentGroupUpdateRequestBody{
Comment: d.Get(mkResourceVirtualEnvironmentGroupComment).(string), Comment: &comment,
} }
groupID := d.Id()
err = veClient.UpdateGroup(groupID, body) err = veClient.UpdateGroup(groupID, body)
if err != nil { if err != nil {

View File

@ -20,7 +20,7 @@ func resourceVirtualEnvironmentRole() *schema.Resource {
return &schema.Resource{ return &schema.Resource{
Schema: map[string]*schema.Schema{ Schema: map[string]*schema.Schema{
mkResourceVirtualEnvironmentRolePrivileges: &schema.Schema{ mkResourceVirtualEnvironmentRolePrivileges: &schema.Schema{
Type: schema.TypeList, Type: schema.TypeSet,
Description: "The role privileges", Description: "The role privileges",
Required: true, Required: true,
Elem: &schema.Schema{Type: schema.TypeString}, Elem: &schema.Schema{Type: schema.TypeString},
@ -46,10 +46,9 @@ func resourceVirtualEnvironmentRoleCreate(d *schema.ResourceData, m interface{})
return err return err
} }
privileges := d.Get(mkResourceVirtualEnvironmentRolePrivileges).([]interface{}) privileges := d.Get(mkResourceVirtualEnvironmentRolePrivileges).(*schema.Set).List()
roleID := d.Get(mkResourceVirtualEnvironmentRoleRoleID).(string)
customPrivileges := make(proxmox.CustomPrivileges, len(privileges)) customPrivileges := make(proxmox.CustomPrivileges, len(privileges))
roleID := d.Get(mkResourceVirtualEnvironmentRoleRoleID).(string)
for i, v := range privileges { for i, v := range privileges {
customPrivileges[i] = v.(string) customPrivileges[i] = v.(string)
@ -92,9 +91,17 @@ func resourceVirtualEnvironmentRoleRead(d *schema.ResourceData, m interface{}) e
return err return err
} }
privileges := schema.NewSet(schema.HashString, make([]interface{}, 0))
if *accessRole != nil {
for _, v := range *accessRole {
privileges.Add(v)
}
}
d.SetId(roleID) d.SetId(roleID)
d.Set(mkResourceVirtualEnvironmentRolePrivileges, *accessRole) d.Set(mkResourceVirtualEnvironmentRolePrivileges, privileges)
return nil return nil
} }
@ -107,8 +114,9 @@ func resourceVirtualEnvironmentRoleUpdate(d *schema.ResourceData, m interface{})
return err return err
} }
privileges := d.Get(mkResourceVirtualEnvironmentRolePrivileges).([]interface{}) privileges := d.Get(mkResourceVirtualEnvironmentRolePrivileges).(*schema.Set).List()
customPrivileges := make(proxmox.CustomPrivileges, len(privileges)) customPrivileges := make(proxmox.CustomPrivileges, len(privileges))
roleID := d.Id()
for i, v := range privileges { for i, v := range privileges {
customPrivileges[i] = v.(string) customPrivileges[i] = v.(string)
@ -118,7 +126,6 @@ func resourceVirtualEnvironmentRoleUpdate(d *schema.ResourceData, m interface{})
Privileges: customPrivileges, Privileges: customPrivileges,
} }
roleID := d.Id()
err = veClient.UpdateRole(roleID, body) err = veClient.UpdateRole(roleID, body)
if err != nil { if err != nil {