github.com/1and1/oneandone-cloudserver-sdk-go@v1.4.1/roles.go (about)

     1  package oneandone
     2  
     3  import "net/http"
     4  
     5  type Role struct {
     6  	Identity
     7  	descField
     8  	CreationDate string       `json:"creation_date,omitempty"`
     9  	State        string       `json:"state,omitempty"`
    10  	Default      *int         `json:"default,omitempty"`
    11  	Permissions  *Permissions `json:"permissions,omitempty"`
    12  	Users        []Identity   `json:"users,omitempty"`
    13  	ApiPtr
    14  }
    15  
    16  type Permissions struct {
    17  	Backups         *BackupPerm         `json:"backups,omitempty"`
    18  	Firewalls       *FirewallPerm       `json:"firewall_policies,omitempty"`
    19  	Images          *ImagePerm          `json:"images,omitempty"`
    20  	Invoice         *InvoicePerm        `json:"interactive_invoices,omitempty"`
    21  	IPs             *IPPerm             `json:"public_ips,omitempty"`
    22  	LoadBalancers   *LoadBalancerPerm   `json:"load_balancers,omitempty"`
    23  	Logs            *LogPerm            `json:"logs,omitempty"`
    24  	MonitorCenter   *MonitorCenterPerm  `json:"monitoring_center,omitempty"`
    25  	MonitorPolicies *MonitorPolicyPerm  `json:"monitoring_policies,omitempty"`
    26  	PrivateNetworks *PrivateNetworkPerm `json:"private_networks,omitempty"`
    27  	Roles           *RolePerm           `json:"roles,omitempty"`
    28  	Servers         *ServerPerm         `json:"servers,omitempty"`
    29  	SharedStorage   *SharedStoragePerm  `json:"shared_storages,omitempty"`
    30  	Usages          *UsagePerm          `json:"usages,omitempty"`
    31  	Users           *UserPerm           `json:"users,omitempty"`
    32  	VPNs            *VPNPerm            `json:"vpn,omitempty"`
    33  }
    34  
    35  type BackupPerm struct {
    36  	Create bool `json:"create"`
    37  	Delete bool `json:"delete"`
    38  	Show   bool `json:"show"`
    39  }
    40  
    41  type FirewallPerm struct {
    42  	Clone                   bool `json:"clone"`
    43  	Create                  bool `json:"create"`
    44  	Delete                  bool `json:"delete"`
    45  	ManageAttachedServerIPs bool `json:"manage_attached_server_ips"`
    46  	ManageRules             bool `json:"manage_rules"`
    47  	SetDescription          bool `json:"set_description"`
    48  	SetName                 bool `json:"set_name"`
    49  	Show                    bool `json:"show"`
    50  }
    51  
    52  type ImagePerm struct {
    53  	Create            bool `json:"create"`
    54  	Delete            bool `json:"delete"`
    55  	DisableAutoCreate bool `json:"disable_automatic_creation"`
    56  	SetDescription    bool `json:"set_description"`
    57  	SetName           bool `json:"set_name"`
    58  	Show              bool `json:"show"`
    59  }
    60  
    61  type InvoicePerm struct {
    62  	Show bool `json:"show"`
    63  }
    64  
    65  type IPPerm struct {
    66  	Create        bool `json:"create"`
    67  	Delete        bool `json:"delete"`
    68  	Release       bool `json:"release"`
    69  	SetReverseDNS bool `json:"set_reverse_dns"`
    70  	Show          bool `json:"show"`
    71  }
    72  
    73  type LoadBalancerPerm struct {
    74  	Create                  bool `json:"create"`
    75  	Delete                  bool `json:"delete"`
    76  	ManageAttachedServerIPs bool `json:"manage_attached_server_ips"`
    77  	ManageRules             bool `json:"manage_rules"`
    78  	Modify                  bool `json:"modify"`
    79  	SetDescription          bool `json:"set_description"`
    80  	SetName                 bool `json:"set_name"`
    81  	Show                    bool `json:"show"`
    82  }
    83  
    84  type LogPerm struct {
    85  	Show bool `json:"show"`
    86  }
    87  
    88  type MonitorCenterPerm struct {
    89  	Show bool `json:"show"`
    90  }
    91  
    92  type MonitorPolicyPerm struct {
    93  	Clone                 bool `json:"clone"`
    94  	Create                bool `json:"create"`
    95  	Delete                bool `json:"delete"`
    96  	ManageAttachedServers bool `json:"manage_attached_servers"`
    97  	ManagePorts           bool `json:"manage_ports"`
    98  	ManageProcesses       bool `json:"manage_processes"`
    99  	ModifyResources       bool `json:"modify_resources"`
   100  	SetDescription        bool `json:"set_description"`
   101  	SetEmail              bool `json:"set_email"`
   102  	SetName               bool `json:"set_name"`
   103  	Show                  bool `json:"show"`
   104  }
   105  
   106  type PrivateNetworkPerm struct {
   107  	Create                bool `json:"create"`
   108  	Delete                bool `json:"delete"`
   109  	ManageAttachedServers bool `json:"manage_attached_servers"`
   110  	SetDescription        bool `json:"set_description"`
   111  	SetName               bool `json:"set_name"`
   112  	SetNetworkInfo        bool `json:"set_network_info"`
   113  	Show                  bool `json:"show"`
   114  }
   115  
   116  type RolePerm struct {
   117  	Clone          bool `json:"clone"`
   118  	Create         bool `json:"create"`
   119  	Delete         bool `json:"delete"`
   120  	ManageUsers    bool `json:"manage_users"`
   121  	Modify         bool `json:"modify"`
   122  	SetDescription bool `json:"set_description"`
   123  	SetName        bool `json:"set_name"`
   124  	Show           bool `json:"show"`
   125  }
   126  
   127  type ServerPerm struct {
   128  	AccessKVMConsole bool `json:"access_kvm_console"`
   129  	AssignIP         bool `json:"assign_ip"`
   130  	Clone            bool `json:"clone"`
   131  	Create           bool `json:"create"`
   132  	Delete           bool `json:"delete"`
   133  	ManageDVD        bool `json:"manage_dvd"`
   134  	ManageSnapshot   bool `json:"manage_snapshot"`
   135  	Reinstall        bool `json:"reinstall"`
   136  	Resize           bool `json:"resize"`
   137  	Restart          bool `json:"restart"`
   138  	SetDescription   bool `json:"set_description"`
   139  	SetName          bool `json:"set_name"`
   140  	Show             bool `json:"show"`
   141  	Shutdown         bool `json:"shutdown"`
   142  	Start            bool `json:"start"`
   143  }
   144  
   145  type SharedStoragePerm struct {
   146  	Access                bool `json:"access"`
   147  	Create                bool `json:"create"`
   148  	Delete                bool `json:"delete"`
   149  	ManageAttachedServers bool `json:"manage_attached_servers"`
   150  	Resize                bool `json:"resize"`
   151  	SetDescription        bool `json:"set_description"`
   152  	SetName               bool `json:"set_name"`
   153  	Show                  bool `json:"show"`
   154  }
   155  
   156  type UsagePerm struct {
   157  	Show bool `json:"show"`
   158  }
   159  
   160  type UserPerm struct {
   161  	ChangeRole     bool `json:"change_role"`
   162  	Create         bool `json:"create"`
   163  	Delete         bool `json:"delete"`
   164  	Disable        bool `json:"disable"`
   165  	Enable         bool `json:"enable"`
   166  	ManageAPI      bool `json:"manage_api"`
   167  	SetDescription bool `json:"set_description"`
   168  	SetEmail       bool `json:"set_email"`
   169  	SetPassword    bool `json:"set_password"`
   170  	Show           bool `json:"show"`
   171  }
   172  
   173  type VPNPerm struct {
   174  	Create         bool `json:"create"`
   175  	Delete         bool `json:"delete"`
   176  	DownloadFile   bool `json:"download_file"`
   177  	SetDescription bool `json:"set_description"`
   178  	SetName        bool `json:"set_name"`
   179  	Show           bool `json:"show"`
   180  }
   181  
   182  // GET /roles
   183  func (api *API) ListRoles(args ...interface{}) ([]Role, error) {
   184  	url, err := processQueryParams(createUrl(api, rolePathSegment), args...)
   185  	if err != nil {
   186  		return nil, err
   187  	}
   188  	result := []Role{}
   189  	err = api.Client.Get(url, &result, http.StatusOK)
   190  	if err != nil {
   191  		return nil, err
   192  	}
   193  	for _, role := range result {
   194  		role.api = api
   195  	}
   196  	return result, nil
   197  }
   198  
   199  // POST /roles
   200  func (api *API) CreateRole(name string) (string, *Role, error) {
   201  	result := new(Role)
   202  	url := createUrl(api, rolePathSegment)
   203  	req := struct {
   204  		Name string `json:"name"`
   205  	}{name}
   206  	err := api.Client.Post(url, &req, &result, http.StatusCreated)
   207  	if err != nil {
   208  		return "", nil, err
   209  	}
   210  	result.api = api
   211  	return result.Id, result, nil
   212  }
   213  
   214  // GET /roles/{role_id}
   215  func (api *API) GetRole(role_id string) (*Role, error) {
   216  	result := new(Role)
   217  	url := createUrl(api, rolePathSegment, role_id)
   218  	err := api.Client.Get(url, &result, http.StatusOK)
   219  	if err != nil {
   220  		return nil, err
   221  	}
   222  	result.api = api
   223  	return result, nil
   224  }
   225  
   226  // PUT /roles/{role_id}
   227  func (api *API) ModifyRole(role_id string, name string, description string, state string) (*Role, error) {
   228  	result := new(Role)
   229  	url := createUrl(api, rolePathSegment, role_id)
   230  	req := struct {
   231  		Name        string `json:"name,omitempty"`
   232  		Description string `json:"description,omitempty"`
   233  		State       string `json:"state,omitempty"`
   234  	}{Name: name, Description: description, State: state}
   235  	err := api.Client.Put(url, &req, &result, http.StatusOK)
   236  	if err != nil {
   237  		return nil, err
   238  	}
   239  	result.api = api
   240  	return result, nil
   241  }
   242  
   243  // DELETE /roles/{role_id}
   244  func (api *API) DeleteRole(role_id string) (*Role, error) {
   245  	result := new(Role)
   246  	url := createUrl(api, rolePathSegment, role_id)
   247  	err := api.Client.Delete(url, nil, &result, http.StatusOK)
   248  	if err != nil {
   249  		return nil, err
   250  	}
   251  	result.api = api
   252  	return result, nil
   253  }
   254  
   255  // GET /roles/{role_id}/permissions
   256  func (api *API) GetRolePermissions(role_id string) (*Permissions, error) {
   257  	result := new(Permissions)
   258  	url := createUrl(api, rolePathSegment, role_id, "permissions")
   259  	err := api.Client.Get(url, &result, http.StatusOK)
   260  	if err != nil {
   261  		return nil, err
   262  	}
   263  	return result, nil
   264  }
   265  
   266  // PUT /roles/{role_id}/permissions
   267  func (api *API) ModifyRolePermissions(role_id string, perm *Permissions) (*Role, error) {
   268  	result := new(Role)
   269  	url := createUrl(api, rolePathSegment, role_id, "permissions")
   270  	err := api.Client.Put(url, &perm, &result, http.StatusOK)
   271  	if err != nil {
   272  		return nil, err
   273  	}
   274  	result.api = api
   275  	return result, nil
   276  }
   277  
   278  // GET /roles/{role_id}/users
   279  func (api *API) ListRoleUsers(role_id string) ([]Identity, error) {
   280  	result := []Identity{}
   281  	url := createUrl(api, rolePathSegment, role_id, "users")
   282  	err := api.Client.Get(url, &result, http.StatusOK)
   283  	if err != nil {
   284  		return nil, err
   285  	}
   286  	return result, nil
   287  }
   288  
   289  // POST /roles/{role_id}/users
   290  func (api *API) AssignRoleUsers(role_id string, user_ids []string) (*Role, error) {
   291  	result := new(Role)
   292  	url := createUrl(api, rolePathSegment, role_id, "users")
   293  	req := struct {
   294  		Users []string `json:"users"`
   295  	}{user_ids}
   296  	err := api.Client.Post(url, &req, &result, http.StatusCreated)
   297  	if err != nil {
   298  		return nil, err
   299  	}
   300  	result.api = api
   301  	return result, nil
   302  }
   303  
   304  // GET /roles/{role_id}/users/{user_id}
   305  func (api *API) GetRoleUser(role_id string, user_id string) (*Identity, error) {
   306  	result := new(Identity)
   307  	url := createUrl(api, rolePathSegment, role_id, "users", user_id)
   308  	err := api.Client.Get(url, &result, http.StatusOK)
   309  	if err != nil {
   310  		return nil, err
   311  	}
   312  	return result, nil
   313  }
   314  
   315  // DELETE /roles/{role_id}/users/{user_id}
   316  func (api *API) RemoveRoleUser(role_id string, user_id string) (*Role, error) {
   317  	result := new(Role)
   318  	url := createUrl(api, rolePathSegment, role_id, "users", user_id)
   319  	err := api.Client.Delete(url, nil, &result, http.StatusAccepted)
   320  	if err != nil {
   321  		return nil, err
   322  	}
   323  	result.api = api
   324  	return result, nil
   325  }
   326  
   327  // POST /roles/{role_id}/clone
   328  func (api *API) CloneRole(role_id string, name string) (*Role, error) {
   329  	result := new(Role)
   330  	url := createUrl(api, rolePathSegment, role_id, "clone")
   331  	req := struct {
   332  		Name string `json:"name"`
   333  	}{name}
   334  	err := api.Client.Post(url, &req, &result, http.StatusCreated)
   335  	if err != nil {
   336  		return nil, err
   337  	}
   338  	result.api = api
   339  	return result, nil
   340  }
   341  
   342  func (role *Role) GetState() (string, error) {
   343  	in, err := role.api.GetRole(role.Id)
   344  	if in == nil {
   345  		return "", err
   346  	}
   347  	return in.State, err
   348  }
   349  
   350  // Sets all backups' permissions
   351  func (bp *BackupPerm) SetAll(value bool) {
   352  	bp.Create = value
   353  	bp.Delete = value
   354  	bp.Show = value
   355  }
   356  
   357  // Sets all firewall policies' permissions
   358  func (fp *FirewallPerm) SetAll(value bool) {
   359  	fp.Clone = value
   360  	fp.Create = value
   361  	fp.Delete = value
   362  	fp.ManageAttachedServerIPs = value
   363  	fp.ManageRules = value
   364  	fp.SetDescription = value
   365  	fp.SetName = value
   366  	fp.Show = value
   367  }
   368  
   369  // Sets all images' permissions
   370  func (imp *ImagePerm) SetAll(value bool) {
   371  	imp.Create = value
   372  	imp.Delete = value
   373  	imp.DisableAutoCreate = value
   374  	imp.SetDescription = value
   375  	imp.SetName = value
   376  	imp.Show = value
   377  }
   378  
   379  // Sets all invoice's permissions
   380  func (inp *InvoicePerm) SetAll(value bool) {
   381  	inp.Show = value
   382  }
   383  
   384  // Sets all IPs' permissions
   385  func (ipp *IPPerm) SetAll(value bool) {
   386  	ipp.Create = value
   387  	ipp.Delete = value
   388  	ipp.Release = value
   389  	ipp.SetReverseDNS = value
   390  	ipp.Show = value
   391  }
   392  
   393  // Sets all load balancers' permissions
   394  func (lbp *LoadBalancerPerm) SetAll(value bool) {
   395  	lbp.Create = value
   396  	lbp.Delete = value
   397  	lbp.ManageAttachedServerIPs = value
   398  	lbp.ManageRules = value
   399  	lbp.Modify = value
   400  	lbp.SetDescription = value
   401  	lbp.SetName = value
   402  	lbp.Show = value
   403  }
   404  
   405  // Sets all logs' permissions
   406  func (lp *LogPerm) SetAll(value bool) {
   407  	lp.Show = value
   408  }
   409  
   410  // Sets all monitoring center's permissions
   411  func (mcp *MonitorCenterPerm) SetAll(value bool) {
   412  	mcp.Show = value
   413  }
   414  
   415  // Sets all monitoring policies' permissions
   416  func (mpp *MonitorPolicyPerm) SetAll(value bool) {
   417  	mpp.Clone = value
   418  	mpp.Create = value
   419  	mpp.Delete = value
   420  	mpp.ManageAttachedServers = value
   421  	mpp.ManagePorts = value
   422  	mpp.ManageProcesses = value
   423  	mpp.ModifyResources = value
   424  	mpp.SetDescription = value
   425  	mpp.SetEmail = value
   426  	mpp.SetName = value
   427  	mpp.Show = value
   428  }
   429  
   430  // Sets all private networks' permissions
   431  func (pnp *PrivateNetworkPerm) SetAll(value bool) {
   432  	pnp.Create = value
   433  	pnp.Delete = value
   434  	pnp.ManageAttachedServers = value
   435  	pnp.SetDescription = value
   436  	pnp.SetName = value
   437  	pnp.SetNetworkInfo = value
   438  	pnp.Show = value
   439  }
   440  
   441  // Sets all roles' permissions
   442  func (rp *RolePerm) SetAll(value bool) {
   443  	rp.Clone = value
   444  	rp.Create = value
   445  	rp.Delete = value
   446  	rp.ManageUsers = value
   447  	rp.Modify = value
   448  	rp.SetDescription = value
   449  	rp.SetName = value
   450  	rp.Show = value
   451  }
   452  
   453  // Sets all servers' permissions
   454  func (sp *ServerPerm) SetAll(value bool) {
   455  	sp.AccessKVMConsole = value
   456  	sp.AssignIP = value
   457  	sp.Clone = value
   458  	sp.Create = value
   459  	sp.Delete = value
   460  	sp.ManageDVD = value
   461  	sp.ManageSnapshot = value
   462  	sp.Reinstall = value
   463  	sp.Resize = value
   464  	sp.Restart = value
   465  	sp.SetDescription = value
   466  	sp.SetName = value
   467  	sp.Show = value
   468  	sp.Shutdown = value
   469  	sp.Start = value
   470  }
   471  
   472  // Sets all shared storages' permissions
   473  func (ssp *SharedStoragePerm) SetAll(value bool) {
   474  	ssp.Access = value
   475  	ssp.Create = value
   476  	ssp.Delete = value
   477  	ssp.ManageAttachedServers = value
   478  	ssp.Resize = value
   479  	ssp.SetDescription = value
   480  	ssp.SetName = value
   481  	ssp.Show = value
   482  }
   483  
   484  // Sets all usages' permissions
   485  func (up *UsagePerm) SetAll(value bool) {
   486  	up.Show = value
   487  }
   488  
   489  // Sets all users' permissions
   490  func (up *UserPerm) SetAll(value bool) {
   491  	up.ChangeRole = value
   492  	up.Create = value
   493  	up.Delete = value
   494  	up.Disable = value
   495  	up.Enable = value
   496  	up.ManageAPI = value
   497  	up.SetDescription = value
   498  	up.SetEmail = value
   499  	up.SetPassword = value
   500  	up.Show = value
   501  }
   502  
   503  // Sets all VPNs' permissions
   504  func (vpnp *VPNPerm) SetAll(value bool) {
   505  	vpnp.Create = value
   506  	vpnp.Delete = value
   507  	vpnp.DownloadFile = value
   508  	vpnp.SetDescription = value
   509  	vpnp.SetName = value
   510  	vpnp.Show = value
   511  }
   512  
   513  // Sets all available permissions
   514  func (p *Permissions) SetAll(v bool) {
   515  	if p.Backups == nil {
   516  		p.Backups = &BackupPerm{v, v, v}
   517  	} else {
   518  		p.Backups.SetAll(v)
   519  	}
   520  	if p.Firewalls == nil {
   521  		p.Firewalls = &FirewallPerm{v, v, v, v, v, v, v, v}
   522  	} else {
   523  		p.Firewalls.SetAll(v)
   524  	}
   525  	if p.Images == nil {
   526  		p.Images = &ImagePerm{v, v, v, v, v, v}
   527  	} else {
   528  		p.Images.SetAll(v)
   529  	}
   530  	if p.Invoice == nil {
   531  		p.Invoice = &InvoicePerm{v}
   532  	} else {
   533  		p.Invoice.SetAll(v)
   534  	}
   535  	if p.IPs == nil {
   536  		p.IPs = &IPPerm{v, v, v, v, v}
   537  	} else {
   538  		p.IPs.SetAll(v)
   539  	}
   540  	if p.LoadBalancers == nil {
   541  		p.LoadBalancers = &LoadBalancerPerm{v, v, v, v, v, v, v, v}
   542  	} else {
   543  		p.LoadBalancers.SetAll(v)
   544  	}
   545  	if p.Logs == nil {
   546  		p.Logs = &LogPerm{v}
   547  	} else {
   548  		p.Logs.SetAll(v)
   549  	}
   550  	if p.MonitorCenter == nil {
   551  		p.MonitorCenter = &MonitorCenterPerm{v}
   552  	} else {
   553  		p.MonitorCenter.SetAll(v)
   554  	}
   555  	if p.MonitorPolicies == nil {
   556  		p.MonitorPolicies = &MonitorPolicyPerm{v, v, v, v, v, v, v, v, v, v, v}
   557  	} else {
   558  		p.MonitorPolicies.SetAll(v)
   559  	}
   560  	if p.PrivateNetworks == nil {
   561  		p.PrivateNetworks = &PrivateNetworkPerm{v, v, v, v, v, v, v}
   562  	} else {
   563  		p.PrivateNetworks.SetAll(v)
   564  	}
   565  	if p.Roles == nil {
   566  		p.Roles = &RolePerm{v, v, v, v, v, v, v, v}
   567  	} else {
   568  		p.Roles.SetAll(v)
   569  	}
   570  	if p.Servers == nil {
   571  		p.Servers = &ServerPerm{v, v, v, v, v, v, v, v, v, v, v, v, v, v, v}
   572  	} else {
   573  		p.Servers.SetAll(v)
   574  	}
   575  	if p.SharedStorage == nil {
   576  		p.SharedStorage = &SharedStoragePerm{v, v, v, v, v, v, v, v}
   577  	} else {
   578  		p.SharedStorage.SetAll(v)
   579  	}
   580  	if p.Usages == nil {
   581  		p.Usages = &UsagePerm{v}
   582  	} else {
   583  		p.Usages.SetAll(v)
   584  	}
   585  	if p.Users == nil {
   586  		p.Users = &UserPerm{v, v, v, v, v, v, v, v, v, v}
   587  	} else {
   588  		p.Users.SetAll(v)
   589  	}
   590  	if p.VPNs == nil {
   591  		p.VPNs = &VPNPerm{v, v, v, v, v, v}
   592  	} else {
   593  		p.VPNs.SetAll(v)
   594  	}
   595  }