mirror of
https://github.com/bpg/terraform-provider-proxmox.git
synced 2025-07-02 03:22:59 +00:00
152 lines
4.4 KiB
Go
152 lines
4.4 KiB
Go
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
|
|
|
package main
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
)
|
|
|
|
const (
|
|
mkDataSourceVirtualEnvironmentUserComment = "comment"
|
|
mkDataSourceVirtualEnvironmentUserEmail = "email"
|
|
mkDataSourceVirtualEnvironmentUserEnabled = "enabled"
|
|
mkDataSourceVirtualEnvironmentUserExpirationDate = "expiration_date"
|
|
mkDataSourceVirtualEnvironmentUserFirstName = "first_name"
|
|
mkDataSourceVirtualEnvironmentUserGroups = "groups"
|
|
mkDataSourceVirtualEnvironmentUserKeys = "keys"
|
|
mkDataSourceVirtualEnvironmentUserLastName = "last_name"
|
|
mkDataSourceVirtualEnvironmentUserUserID = "user_id"
|
|
)
|
|
|
|
func dataSourceVirtualEnvironmentUser() *schema.Resource {
|
|
return &schema.Resource{
|
|
Schema: map[string]*schema.Schema{
|
|
mkDataSourceVirtualEnvironmentUserComment: &schema.Schema{
|
|
Type: schema.TypeString,
|
|
Description: "The user comment",
|
|
Computed: true,
|
|
},
|
|
mkDataSourceVirtualEnvironmentUserEmail: &schema.Schema{
|
|
Type: schema.TypeString,
|
|
Description: "The user's email address",
|
|
Computed: true,
|
|
},
|
|
mkDataSourceVirtualEnvironmentUserEnabled: &schema.Schema{
|
|
Type: schema.TypeBool,
|
|
Description: "Whether the user account is enabled",
|
|
Computed: true,
|
|
},
|
|
mkDataSourceVirtualEnvironmentUserExpirationDate: &schema.Schema{
|
|
Type: schema.TypeString,
|
|
Description: "The user account's expiration date",
|
|
Computed: true,
|
|
},
|
|
mkDataSourceVirtualEnvironmentUserFirstName: &schema.Schema{
|
|
Type: schema.TypeString,
|
|
Description: "The user's first name",
|
|
Computed: true,
|
|
},
|
|
mkDataSourceVirtualEnvironmentUserGroups: &schema.Schema{
|
|
Type: schema.TypeList,
|
|
Description: "The user's groups",
|
|
Computed: true,
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
},
|
|
mkDataSourceVirtualEnvironmentUserKeys: &schema.Schema{
|
|
Type: schema.TypeString,
|
|
Description: "The user's keys",
|
|
Computed: true,
|
|
},
|
|
mkDataSourceVirtualEnvironmentUserLastName: &schema.Schema{
|
|
Type: schema.TypeString,
|
|
Description: "The user's last name",
|
|
Computed: true,
|
|
},
|
|
mkDataSourceVirtualEnvironmentUserUserID: &schema.Schema{
|
|
Type: schema.TypeString,
|
|
Description: "The user id",
|
|
Required: true,
|
|
},
|
|
},
|
|
Read: dataSourceVirtualEnvironmentUserRead,
|
|
}
|
|
}
|
|
|
|
func dataSourceVirtualEnvironmentUserRead(d *schema.ResourceData, m interface{}) error {
|
|
config := m.(providerConfiguration)
|
|
veClient, err := config.GetVEClient()
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
userID := d.Get(mkDataSourceVirtualEnvironmentUserUserID).(string)
|
|
v, err := veClient.GetUser(userID)
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
d.SetId(userID)
|
|
|
|
if v.Comment != nil {
|
|
d.Set(mkDataSourceVirtualEnvironmentUserComment, v.Comment)
|
|
} else {
|
|
d.Set(mkDataSourceVirtualEnvironmentUserComment, "")
|
|
}
|
|
|
|
if v.Email != nil {
|
|
d.Set(mkDataSourceVirtualEnvironmentUserEmail, v.Email)
|
|
} else {
|
|
d.Set(mkDataSourceVirtualEnvironmentUserEmail, "")
|
|
}
|
|
|
|
if v.Enabled != nil {
|
|
d.Set(mkDataSourceVirtualEnvironmentUserEnabled, v.Enabled)
|
|
} else {
|
|
d.Set(mkDataSourceVirtualEnvironmentUserEnabled, true)
|
|
}
|
|
|
|
if v.ExpirationDate != nil {
|
|
t := time.Time(*v.ExpirationDate)
|
|
|
|
if t.Unix() > 0 {
|
|
d.Set(mkDataSourceVirtualEnvironmentUserExpirationDate, t.UTC().Format(time.RFC3339))
|
|
} else {
|
|
d.Set(mkDataSourceVirtualEnvironmentUserExpirationDate, time.Unix(2147483647, 0).UTC().Format(time.RFC3339))
|
|
}
|
|
} else {
|
|
d.Set(mkDataSourceVirtualEnvironmentUserExpirationDate, time.Unix(2147483647, 0).UTC().Format(time.RFC3339))
|
|
}
|
|
|
|
if v.FirstName != nil {
|
|
d.Set(mkDataSourceVirtualEnvironmentUserFirstName, v.FirstName)
|
|
} else {
|
|
d.Set(mkDataSourceVirtualEnvironmentUserFirstName, "")
|
|
}
|
|
|
|
if v.Groups != nil {
|
|
d.Set(mkDataSourceVirtualEnvironmentUserGroups, v.Groups)
|
|
} else {
|
|
d.Set(mkDataSourceVirtualEnvironmentUserGroups, "")
|
|
}
|
|
|
|
if v.Keys != nil {
|
|
d.Set(mkDataSourceVirtualEnvironmentUserGroups, v.Keys)
|
|
} else {
|
|
d.Set(mkDataSourceVirtualEnvironmentUserGroups, "")
|
|
}
|
|
|
|
if v.LastName != nil {
|
|
d.Set(mkDataSourceVirtualEnvironmentUserLastName, v.LastName)
|
|
} else {
|
|
d.Set(mkDataSourceVirtualEnvironmentUserLastName, "")
|
|
}
|
|
|
|
return nil
|
|
}
|