mirror of
https://github.com/opentofu/opentofu.git
synced 2025-01-08 15:13:56 -06:00
987b910828
* vendor: update gopkg.in/ns1/ns1-go.v2 * provider/ns1: Port the ns1 provider to Terraform core * docs/ns1: Document the ns1 provider * ns1: rename remaining nsone -> ns1 (#10805) * Ns1 provider (#11300) * provider/ns1: Flesh out support for meta structs. Following the structure outlined by @pashap. Using reflection to reduce copy/paste. Putting metas inside single-item lists. This is clunky, but I couldn't figure out how else to have a nested struct. Maybe the Terraform people know a better way? Inside the meta struct, all fields are always written to the state; I can't figure out how to omit fields that aren't used. This is not just verbose, it actually causes issues because you can't have both "up" and "up_feed" set). Also some minor other changes: - Add "terraform" import support to records and zones. - Create helper class StringEnum. * provider/ns1: Make fmt * provider/ns1: Remove stubbed out RecordRead (used for testing metadata change). * provider/ns1: Need to get interface that m contains from Ptr Value with Elem() * provider/ns1: Use empty string to indicate no feed given. * provider/ns1: Remove old record.regions fields. * provider/ns1: Removes redundant testAccCheckRecordState * provider/ns1: Moves account permissions logic to permissions.go * provider/ns1: Adds tests for team resource. * provider/ns1: Move remaining permissions logic to permissions.go * ns1/provider: Adds datasource.config * provider/ns1: Small clean up of datafeed resource tests * provider/ns1: removes testAccCheckZoneState in favor of explicit name check * provider/ns1: More renaming of nsone -> ns1 * provider/ns1: Comment out metadata for the moment. * Ns1 provider (#11347) * Fix the removal of empty containers from a flatmap Removal of empty nested containers from a flatmap would sometimes fail a sanity check when removed in the wrong order. This would only fail sometimes due to map iteration. There was also an off-by-one error in the prefix check which could match the incorrect keys. * provider/ns1: Adds ns1 go client through govendor. * provider/ns1: Removes unused debug line * docs/ns1: Adds docs around apikey/datasource/datafeed/team/user/record. * provider/ns1: Gets go vet green
134 lines
3.1 KiB
Go
134 lines
3.1 KiB
Go
package ns1
|
|
|
|
import (
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
ns1 "gopkg.in/ns1/ns1-go.v2/rest"
|
|
"gopkg.in/ns1/ns1-go.v2/rest/model/account"
|
|
)
|
|
|
|
func userResource() *schema.Resource {
|
|
s := map[string]*schema.Schema{
|
|
"id": &schema.Schema{
|
|
Type: schema.TypeString,
|
|
Computed: true,
|
|
},
|
|
"name": &schema.Schema{
|
|
Type: schema.TypeString,
|
|
Required: true,
|
|
},
|
|
"username": &schema.Schema{
|
|
Type: schema.TypeString,
|
|
Required: true,
|
|
},
|
|
"email": &schema.Schema{
|
|
Type: schema.TypeString,
|
|
Required: true,
|
|
},
|
|
"notify": &schema.Schema{
|
|
Type: schema.TypeMap,
|
|
Optional: true,
|
|
Elem: &schema.Resource{
|
|
Schema: map[string]*schema.Schema{
|
|
"billing": &schema.Schema{
|
|
Type: schema.TypeBool,
|
|
Required: true,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
"teams": &schema.Schema{
|
|
Type: schema.TypeList,
|
|
Optional: true,
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
},
|
|
}
|
|
s = addPermsSchema(s)
|
|
return &schema.Resource{
|
|
Schema: s,
|
|
Create: UserCreate,
|
|
Read: UserRead,
|
|
Update: UserUpdate,
|
|
Delete: UserDelete,
|
|
}
|
|
}
|
|
|
|
func userToResourceData(d *schema.ResourceData, u *account.User) error {
|
|
d.SetId(u.Username)
|
|
d.Set("name", u.Name)
|
|
d.Set("email", u.Email)
|
|
d.Set("teams", u.TeamIDs)
|
|
notify := make(map[string]bool)
|
|
notify["billing"] = u.Notify.Billing
|
|
d.Set("notify", notify)
|
|
permissionsToResourceData(d, u.Permissions)
|
|
return nil
|
|
}
|
|
|
|
func resourceDataToUser(u *account.User, d *schema.ResourceData) error {
|
|
u.Name = d.Get("name").(string)
|
|
u.Username = d.Get("username").(string)
|
|
u.Email = d.Get("email").(string)
|
|
if v, ok := d.GetOk("teams"); ok {
|
|
teamsRaw := v.([]interface{})
|
|
u.TeamIDs = make([]string, len(teamsRaw))
|
|
for i, team := range teamsRaw {
|
|
u.TeamIDs[i] = team.(string)
|
|
}
|
|
} else {
|
|
u.TeamIDs = make([]string, 0)
|
|
}
|
|
if v, ok := d.GetOk("notify"); ok {
|
|
notifyRaw := v.(map[string]interface{})
|
|
u.Notify.Billing = notifyRaw["billing"].(bool)
|
|
}
|
|
u.Permissions = resourceDataToPermissions(d)
|
|
return nil
|
|
}
|
|
|
|
// UserCreate creates the given user in ns1
|
|
func UserCreate(d *schema.ResourceData, meta interface{}) error {
|
|
client := meta.(*ns1.Client)
|
|
u := account.User{}
|
|
if err := resourceDataToUser(&u, d); err != nil {
|
|
return err
|
|
}
|
|
if _, err := client.Users.Create(&u); err != nil {
|
|
return err
|
|
}
|
|
return userToResourceData(d, &u)
|
|
}
|
|
|
|
// UserRead reads the given users data from ns1
|
|
func UserRead(d *schema.ResourceData, meta interface{}) error {
|
|
client := meta.(*ns1.Client)
|
|
u, _, err := client.Users.Get(d.Id())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return userToResourceData(d, u)
|
|
}
|
|
|
|
// UserDelete deletes the given user from ns1
|
|
func UserDelete(d *schema.ResourceData, meta interface{}) error {
|
|
client := meta.(*ns1.Client)
|
|
_, err := client.Users.Delete(d.Id())
|
|
d.SetId("")
|
|
return err
|
|
}
|
|
|
|
// UserUpdate updates the user with given parameters in ns1
|
|
func UserUpdate(d *schema.ResourceData, meta interface{}) error {
|
|
client := meta.(*ns1.Client)
|
|
u := account.User{
|
|
Username: d.Id(),
|
|
}
|
|
if err := resourceDataToUser(&u, d); err != nil {
|
|
return err
|
|
}
|
|
if _, err := client.Users.Update(&u); err != nil {
|
|
return err
|
|
}
|
|
return userToResourceData(d, &u)
|
|
}
|