diff --git a/pkg/api/users.go b/pkg/api/users.go index 04af83e..03cab5e 100644 --- a/pkg/api/users.go +++ b/pkg/api/users.go @@ -167,14 +167,17 @@ func (u *Users) Delete(ctx context.Context, userKey string) error { // AssignRole assigns a role to a user in your context's environment, by user key, role key and tenant key. // Usage Example: -// `roleAssignment, err := PermitClient.Api.Users.AssignRole(ctx, "user-key", "role-key", "default")` -func (u *Users) AssignRole(ctx context.Context, userKey string, roleKey string, tenantKey string) (*models.RoleAssignmentRead, error) { +// `roleAssignment, err := PermitClient.Api.Users.AssignRole(ctx, "user-key", "role-key", "default", "document:mydoc")` +func (u *Users) AssignRole(ctx context.Context, userKey string, roleKey string, tenantKey string, resourceInstance *string) (*models.RoleAssignmentRead, error) { err := u.lazyLoadPermitContext(ctx) if err != nil { u.logger.Error("", zap.Error(err)) return nil, err } userRoleCreate := *models.NewUserRoleCreate(roleKey, tenantKey) + if resourceInstance != nil { + userRoleCreate.SetResourceInstance(*resourceInstance) + } roleAssignmentRead, httpRes, err := u.client.UsersApi.AssignRoleToUser(ctx, u.config.Context.GetProject(), u.config.Context.GetEnvironment(), userKey).UserRoleCreate(userRoleCreate).Execute() err = errors.HttpErrorHandle(err, httpRes) if err != nil { diff --git a/pkg/models/model_role_assignment_create.go b/pkg/models/model_role_assignment_create.go index e5569a1..9c3a10d 100644 --- a/pkg/models/model_role_assignment_create.go +++ b/pkg/models/model_role_assignment_create.go @@ -11,19 +11,28 @@ API version: 2.0.0 package models import ( + "bytes" "encoding/json" + "fmt" ) +// checks if the RoleAssignmentCreate type satisfies the MappedNullable interface at compile time +var _ MappedNullable = &RoleAssignmentCreate{} + // RoleAssignmentCreate struct for RoleAssignmentCreate type RoleAssignmentCreate struct { // the role that will be assigned (accepts either the role id or the role key) Role string `json:"role"` // the tenant the role is associated with (accepts either the tenant id or the tenant key) Tenant string `json:"tenant"` + // the resource instance the role is associated with (accepts either the resource instance id or key using this format resource_type:resource_instance) + ResourceInstance *string `json:"resource_instance,omitempty"` // the user the role will be assigned to (accepts either the user id or the user key) User string `json:"user"` } +type _RoleAssignmentCreate RoleAssignmentCreate + // NewRoleAssignmentCreate instantiates a new RoleAssignmentCreate object // This constructor will assign default values to properties that have it defined, // and makes sure properties required by API are set, but the set of arguments @@ -92,6 +101,38 @@ func (o *RoleAssignmentCreate) SetTenant(v string) { o.Tenant = v } +// GetResourceInstance returns the ResourceInstance field value if set, zero value otherwise. +func (o *RoleAssignmentCreate) GetResourceInstance() string { + if o == nil || IsNil(o.ResourceInstance) { + var ret string + return ret + } + return *o.ResourceInstance +} + +// GetResourceInstanceOk returns a tuple with the ResourceInstance field value if set, nil otherwise +// and a boolean to check if the value has been set. +func (o *RoleAssignmentCreate) GetResourceInstanceOk() (*string, bool) { + if o == nil || IsNil(o.ResourceInstance) { + return nil, false + } + return o.ResourceInstance, true +} + +// HasResourceInstance returns a boolean if a field has been set. +func (o *RoleAssignmentCreate) HasResourceInstance() bool { + if o != nil && !IsNil(o.ResourceInstance) { + return true + } + + return false +} + +// SetResourceInstance gets a reference to the given string and assigns it to the ResourceInstance field. +func (o *RoleAssignmentCreate) SetResourceInstance(v string) { + o.ResourceInstance = &v +} + // GetUser returns the User field value func (o *RoleAssignmentCreate) GetUser() string { if o == nil { @@ -117,17 +158,62 @@ func (o *RoleAssignmentCreate) SetUser(v string) { } func (o RoleAssignmentCreate) MarshalJSON() ([]byte, error) { - toSerialize := map[string]interface{}{} - if true { - toSerialize["role"] = o.Role + toSerialize, err := o.ToMap() + if err != nil { + return []byte{}, err } - if true { + return json.Marshal(toSerialize) +} + +func (o RoleAssignmentCreate) ToMap() (map[string]interface{}, error) { + toSerialize := map[string]interface{}{} + toSerialize["role"] = o.Role + if !IsNil(o.Tenant) { toSerialize["tenant"] = o.Tenant } - if true { - toSerialize["user"] = o.User + if !IsNil(o.ResourceInstance) { + toSerialize["resource_instance"] = o.ResourceInstance } - return json.Marshal(toSerialize) + toSerialize["user"] = o.User + return toSerialize, nil +} + +func (o *RoleAssignmentCreate) UnmarshalJSON(data []byte) (err error) { + // This validates that all required properties are included in the JSON object + // by unmarshalling the object into a generic map with string keys and checking + // that every required field exists as a key in the generic map. + requiredProperties := []string{ + "role", + "user", + } + + allProperties := make(map[string]interface{}) + + err = json.Unmarshal(data, &allProperties) + + if err != nil { + return err + } + + for _, requiredProperty := range requiredProperties { + if _, exists := allProperties[requiredProperty]; !exists { + return fmt.Errorf("no value given for required property %v", requiredProperty) + } + } + + varRoleAssignmentCreate := _RoleAssignmentCreate{} + + decoder := json.NewDecoder(bytes.NewReader(data)) + decoder.DisallowUnknownFields() + err = decoder.Decode(&varRoleAssignmentCreate) + + if err != nil { + return err + } + + *o = RoleAssignmentCreate(varRoleAssignmentCreate) + + return err } type NullableRoleAssignmentCreate struct { diff --git a/pkg/models/model_user_role_create.go b/pkg/models/model_user_role_create.go index 5194a8b..d77261e 100644 --- a/pkg/models/model_user_role_create.go +++ b/pkg/models/model_user_role_create.go @@ -11,17 +11,26 @@ API version: 2.0.0 package models import ( + "bytes" "encoding/json" + "fmt" ) +// checks if the UserRoleCreate type satisfies the MappedNullable interface at compile time +var _ MappedNullable = &UserRoleCreate{} + // UserRoleCreate struct for UserRoleCreate type UserRoleCreate struct { // the role that will be assigned (accepts either the role id or the role key) Role string `json:"role"` // the tenant the role is associated with (accepts either the tenant id or the tenant key) Tenant string `json:"tenant"` + // the resource instance the role is associated with (accepts either the resource instance id or key using this format resource_type:resource_instance) + ResourceInstance *string `json:"resource_instance,omitempty"` } +type _UserRoleCreate UserRoleCreate + // NewUserRoleCreate instantiates a new UserRoleCreate object // This constructor will assign default values to properties that have it defined, // and makes sure properties required by API are set, but the set of arguments @@ -89,15 +98,92 @@ func (o *UserRoleCreate) SetTenant(v string) { o.Tenant = v } +// GetResourceInstance returns the ResourceInstance field value if set, zero value otherwise. +func (o *UserRoleCreate) GetResourceInstance() string { + if o == nil || IsNil(o.ResourceInstance) { + var ret string + return ret + } + return *o.ResourceInstance +} + +// GetResourceInstanceOk returns a tuple with the ResourceInstance field value if set, nil otherwise +// and a boolean to check if the value has been set. +func (o *UserRoleCreate) GetResourceInstanceOk() (*string, bool) { + if o == nil || IsNil(o.ResourceInstance) { + return nil, false + } + return o.ResourceInstance, true +} + +// HasResourceInstance returns a boolean if a field has been set. +func (o *UserRoleCreate) HasResourceInstance() bool { + if o != nil && !IsNil(o.ResourceInstance) { + return true + } + + return false +} + +// SetResourceInstance gets a reference to the given string and assigns it to the ResourceInstance field. +func (o *UserRoleCreate) SetResourceInstance(v string) { + o.ResourceInstance = &v +} + func (o UserRoleCreate) MarshalJSON() ([]byte, error) { + toSerialize, err := o.ToMap() + if err != nil { + return []byte{}, err + } + return json.Marshal(toSerialize) +} + +func (o UserRoleCreate) ToMap() (map[string]interface{}, error) { toSerialize := map[string]interface{}{} - if true { - toSerialize["role"] = o.Role + toSerialize["role"] = o.Role + toSerialize["tenant"] = o.Tenant + if !IsNil(o.ResourceInstance) { + toSerialize["resource_instance"] = o.ResourceInstance } - if true { - toSerialize["tenant"] = o.Tenant + return toSerialize, nil +} + +func (o *UserRoleCreate) UnmarshalJSON(data []byte) (err error) { + // This validates that all required properties are included in the JSON object + // by unmarshalling the object into a generic map with string keys and checking + // that every required field exists as a key in the generic map. + requiredProperties := []string{ + "role", + "tenant", } - return json.Marshal(toSerialize) + + allProperties := make(map[string]interface{}) + + err = json.Unmarshal(data, &allProperties) + + if err != nil { + return err + } + + for _, requiredProperty := range requiredProperties { + if _, exists := allProperties[requiredProperty]; !exists { + return fmt.Errorf("no value given for required property %v", requiredProperty) + } + } + + varUserRoleCreate := _UserRoleCreate{} + + decoder := json.NewDecoder(bytes.NewReader(data)) + decoder.DisallowUnknownFields() + err = decoder.Decode(&varUserRoleCreate) + + if err != nil { + return err + } + + *o = UserRoleCreate(varUserRoleCreate) + + return err } type NullableUserRoleCreate struct {