Skip to content

Commit

Permalink
Add data source for users with email lookup (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
HuwCampbell authored Mar 25, 2024
2 parents 1d27850 + dffbe50 commit c19ef40
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
73 changes: 73 additions & 0 deletions client/data_source_user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package anaml

import (
"strconv"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func DataSourceUser() *schema.Resource {
return &schema.Resource{
Read: dataSourceUserRead,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"email": {
Type: schema.TypeString,
Required: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"given_name": {
Type: schema.TypeString,
Computed: true,
},
"surname": {
Type: schema.TypeString,
Computed: true,
},
"roles": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
}

func dataSourceUserRead(d *schema.ResourceData, m interface{}) error {
c := m.(*Client)
email := d.Get("email").(string)

user, err := c.FindUserByEmail(email)
if err != nil {
return err
}

if user == nil {
d.SetId("")
return nil
}

d.SetId(strconv.Itoa(user.ID))

if err := d.Set("name", user.Name); err != nil {
return err
}
if err := d.Set("given_name", user.GivenName); err != nil {
return err
}
if err := d.Set("surname", user.Surname); err != nil {
return err
}
if err := d.Set("roles", mapRolesToFrontend(user.Roles)); err != nil {
return err
}
return nil
}
27 changes: 27 additions & 0 deletions client/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,33 @@ func (c *Client) GetUser(userID string) (*User, error) {
return &user, nil
}

func (c *Client) FindUserByEmail(email string) (*User, error) {
req, err := http.NewRequest("GET", fmt.Sprintf("%s/user", c.HostURL), nil)
if err != nil {
return nil, err
}

q := req.URL.Query()
q.Add("email", email)
req.URL.RawQuery = q.Encode()

body, err := c.doRequest(req)
if err != nil {
return nil, err
}
if body == nil {
return nil, nil
}

user := User{}
err = json.Unmarshal(body, &user)
if err != nil {
return nil, err
}

return &user, nil
}

func (c *Client) CreateUser(creationRequest User) (*User, error) {
rb, err := json.Marshal(creationRequest)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions providers/terraform-provider-anaml-operations/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func Provider() *schema.Provider {
"anaml-operations_destination": anaml.DataSourceDestination(),
"anaml-operations_source": anaml.DataSourceSource(),
"anaml-operations_feature_store": anaml.DataSourceFeatureStore(),
"anaml-operations_user": anaml.DataSourceUser(),
},

ResourcesMap: map[string]*schema.Resource{
Expand Down

0 comments on commit c19ef40

Please sign in to comment.