diff --git a/client/data_source_user.go b/client/data_source_user.go new file mode 100644 index 0000000..f40eb21 --- /dev/null +++ b/client/data_source_user.go @@ -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 +} diff --git a/client/user.go b/client/user.go index 1b2435d..d7aa67e 100644 --- a/client/user.go +++ b/client/user.go @@ -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 { diff --git a/providers/terraform-provider-anaml-operations/provider.go b/providers/terraform-provider-anaml-operations/provider.go index 6c908c7..0ea734a 100644 --- a/providers/terraform-provider-anaml-operations/provider.go +++ b/providers/terraform-provider-anaml-operations/provider.go @@ -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{