github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/acceptance/openstack/identity/v3/identity.go (about)

     1  package v3
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/huaweicloud/golangsdk"
     7  	"github.com/huaweicloud/golangsdk/acceptance/tools"
     8  	"github.com/huaweicloud/golangsdk/openstack/identity/v3/domains"
     9  	"github.com/huaweicloud/golangsdk/openstack/identity/v3/groups"
    10  	"github.com/huaweicloud/golangsdk/openstack/identity/v3/projects"
    11  	"github.com/huaweicloud/golangsdk/openstack/identity/v3/regions"
    12  	"github.com/huaweicloud/golangsdk/openstack/identity/v3/roles"
    13  	"github.com/huaweicloud/golangsdk/openstack/identity/v3/services"
    14  	"github.com/huaweicloud/golangsdk/openstack/identity/v3/users"
    15  )
    16  
    17  // CreateProject will create a project with a random name.
    18  // It takes an optional createOpts parameter since creating a project
    19  // has so many options. An error will be returned if the project was
    20  // unable to be created.
    21  func CreateProject(t *testing.T, client *golangsdk.ServiceClient, c *projects.CreateOpts) (*projects.Project, error) {
    22  	name := tools.RandomString("ACPTTEST", 8)
    23  	t.Logf("Attempting to create project: %s", name)
    24  
    25  	var createOpts projects.CreateOpts
    26  	if c != nil {
    27  		createOpts = *c
    28  	} else {
    29  		createOpts = projects.CreateOpts{}
    30  	}
    31  
    32  	createOpts.Name = name
    33  
    34  	project, err := projects.Create(client, createOpts).Extract()
    35  	if err != nil {
    36  		return project, err
    37  	}
    38  
    39  	t.Logf("Successfully created project %s with ID %s", name, project.ID)
    40  
    41  	return project, nil
    42  }
    43  
    44  // CreateUser will create a user with a random name.
    45  // It takes an optional createOpts parameter since creating a user
    46  // has so many options. An error will be returned if the user was
    47  // unable to be created.
    48  func CreateUser(t *testing.T, client *golangsdk.ServiceClient, c *users.CreateOpts) (*users.User, error) {
    49  	name := tools.RandomString("ACPTTEST", 8)
    50  	t.Logf("Attempting to create user: %s", name)
    51  
    52  	var createOpts users.CreateOpts
    53  	if c != nil {
    54  		createOpts = *c
    55  	} else {
    56  		createOpts = users.CreateOpts{}
    57  	}
    58  
    59  	createOpts.Name = name
    60  
    61  	user, err := users.Create(client, createOpts).Extract()
    62  	if err != nil {
    63  		return user, err
    64  	}
    65  
    66  	t.Logf("Successfully created user %s with ID %s", name, user.ID)
    67  
    68  	return user, nil
    69  }
    70  
    71  // CreateGroup will create a group with a random name.
    72  // It takes an optional createOpts parameter since creating a group
    73  // has so many options. An error will be returned if the group was
    74  // unable to be created.
    75  func CreateGroup(t *testing.T, client *golangsdk.ServiceClient, c *groups.CreateOpts) (*groups.Group, error) {
    76  	name := tools.RandomString("ACPTTEST", 8)
    77  	t.Logf("Attempting to create group: %s", name)
    78  
    79  	var createOpts groups.CreateOpts
    80  	if c != nil {
    81  		createOpts = *c
    82  	} else {
    83  		createOpts = groups.CreateOpts{}
    84  	}
    85  
    86  	createOpts.Name = name
    87  
    88  	group, err := groups.Create(client, createOpts).Extract()
    89  	if err != nil {
    90  		return group, err
    91  	}
    92  
    93  	t.Logf("Successfully created group %s with ID %s", name, group.ID)
    94  
    95  	return group, nil
    96  }
    97  
    98  // CreateDomain will create a domain with a random name.
    99  // It takes an optional createOpts parameter since creating a domain
   100  // has many options. An error will be returned if the domain was
   101  // unable to be created.
   102  func CreateDomain(t *testing.T, client *golangsdk.ServiceClient, c *domains.CreateOpts) (*domains.Domain, error) {
   103  	name := tools.RandomString("ACPTTEST", 8)
   104  	t.Logf("Attempting to create domain: %s", name)
   105  
   106  	var createOpts domains.CreateOpts
   107  	if c != nil {
   108  		createOpts = *c
   109  	} else {
   110  		createOpts = domains.CreateOpts{}
   111  	}
   112  
   113  	createOpts.Name = name
   114  
   115  	domain, err := domains.Create(client, createOpts).Extract()
   116  	if err != nil {
   117  		return domain, err
   118  	}
   119  
   120  	t.Logf("Successfully created domain %s with ID %s", name, domain.ID)
   121  
   122  	return domain, nil
   123  }
   124  
   125  // CreateRole will create a role with a random name.
   126  // It takes an optional createOpts parameter since creating a role
   127  // has so many options. An error will be returned if the role was
   128  // unable to be created.
   129  func CreateRole(t *testing.T, client *golangsdk.ServiceClient, c *roles.CreateOpts) (*roles.Role, error) {
   130  	name := tools.RandomString("ACPTTEST", 8)
   131  	t.Logf("Attempting to create role: %s", name)
   132  
   133  	var createOpts roles.CreateOpts
   134  	if c != nil {
   135  		createOpts = *c
   136  	} else {
   137  		createOpts = roles.CreateOpts{}
   138  	}
   139  
   140  	createOpts.Name = name
   141  
   142  	role, err := roles.Create(client, createOpts).Extract()
   143  	if err != nil {
   144  		return role, err
   145  	}
   146  
   147  	t.Logf("Successfully created role %s with ID %s", name, role.ID)
   148  
   149  	return role, nil
   150  }
   151  
   152  // CreateRegion will create a region with a random name.
   153  // It takes an optional createOpts parameter since creating a region
   154  // has so many options. An error will be returned if the region was
   155  // unable to be created.
   156  func CreateRegion(t *testing.T, client *golangsdk.ServiceClient, c *regions.CreateOpts) (*regions.Region, error) {
   157  	id := tools.RandomString("ACPTTEST", 8)
   158  	t.Logf("Attempting to create region: %s", id)
   159  
   160  	var createOpts regions.CreateOpts
   161  	if c != nil {
   162  		createOpts = *c
   163  	} else {
   164  		createOpts = regions.CreateOpts{}
   165  	}
   166  
   167  	createOpts.ID = id
   168  
   169  	region, err := regions.Create(client, createOpts).Extract()
   170  	if err != nil {
   171  		return region, err
   172  	}
   173  
   174  	t.Logf("Successfully created region %s", id)
   175  
   176  	return region, nil
   177  }
   178  
   179  // CreateService will create a service with a random name.
   180  // It takes an optional createOpts parameter since creating a service
   181  // has so many options. An error will be returned if the service was
   182  // unable to be created.
   183  func CreateService(t *testing.T, client *golangsdk.ServiceClient, c *services.CreateOpts) (*services.Service, error) {
   184  	name := tools.RandomString("ACPTTEST", 8)
   185  	t.Logf("Attempting to create service: %s", name)
   186  
   187  	var createOpts services.CreateOpts
   188  	if c != nil {
   189  		createOpts = *c
   190  	} else {
   191  		createOpts = services.CreateOpts{}
   192  	}
   193  
   194  	createOpts.Extra["name"] = name
   195  
   196  	service, err := services.Create(client, createOpts).Extract()
   197  	if err != nil {
   198  		return service, err
   199  	}
   200  
   201  	t.Logf("Successfully created service %s", service.ID)
   202  
   203  	return service, nil
   204  }
   205  
   206  // DeleteProject will delete a project by ID. A fatal error will occur if
   207  // the project ID failed to be deleted. This works best when using it as
   208  // a deferred function.
   209  func DeleteProject(t *testing.T, client *golangsdk.ServiceClient, projectID string) {
   210  	err := projects.Delete(client, projectID).ExtractErr()
   211  	if err != nil {
   212  		t.Fatalf("Unable to delete project %s: %v", projectID, err)
   213  	}
   214  
   215  	t.Logf("Deleted project: %s", projectID)
   216  }
   217  
   218  // DeleteUser will delete a user by ID. A fatal error will occur if
   219  // the user failed to be deleted. This works best when using it as
   220  // a deferred function.
   221  func DeleteUser(t *testing.T, client *golangsdk.ServiceClient, userID string) {
   222  	err := users.Delete(client, userID).ExtractErr()
   223  	if err != nil {
   224  		t.Fatalf("Unable to delete user with ID %s: %v", userID, err)
   225  	}
   226  
   227  	t.Logf("Deleted user with ID: %s", userID)
   228  }
   229  
   230  // DeleteGroup will delete a group by ID. A fatal error will occur if
   231  // the group failed to be deleted. This works best when using it as
   232  // a deferred function.
   233  func DeleteGroup(t *testing.T, client *golangsdk.ServiceClient, groupID string) {
   234  	err := groups.Delete(client, groupID).ExtractErr()
   235  	if err != nil {
   236  		t.Fatalf("Unable to delete group %s: %v", groupID, err)
   237  	}
   238  
   239  	t.Logf("Deleted group: %s", groupID)
   240  }
   241  
   242  // DeleteDomain will delete a domain by ID. A fatal error will occur if
   243  // the project ID failed to be deleted. This works best when using it as
   244  // a deferred function.
   245  func DeleteDomain(t *testing.T, client *golangsdk.ServiceClient, domainID string) {
   246  	err := domains.Delete(client, domainID).ExtractErr()
   247  	if err != nil {
   248  		t.Fatalf("Unable to delete domain %s: %v", domainID, err)
   249  	}
   250  
   251  	t.Logf("Deleted domain: %s", domainID)
   252  }
   253  
   254  // DeleteRole will delete a role by ID. A fatal error will occur if
   255  // the role failed to be deleted. This works best when using it as
   256  // a deferred function.
   257  func DeleteRole(t *testing.T, client *golangsdk.ServiceClient, roleID string) {
   258  	err := roles.Delete(client, roleID).ExtractErr()
   259  	if err != nil {
   260  		t.Fatalf("Unable to delete role %s: %v", roleID, err)
   261  	}
   262  
   263  	t.Logf("Deleted role: %s", roleID)
   264  }
   265  
   266  // DeleteRegion will delete a reg by ID. A fatal error will occur if
   267  // the region failed to be deleted. This works best when using it as
   268  // a deferred function.
   269  func DeleteRegion(t *testing.T, client *golangsdk.ServiceClient, regionID string) {
   270  	err := regions.Delete(client, regionID).ExtractErr()
   271  	if err != nil {
   272  		t.Fatalf("Unable to delete region %s: %v", regionID, err)
   273  	}
   274  
   275  	t.Logf("Deleted region: %s", regionID)
   276  }
   277  
   278  // DeleteService will delete a reg by ID. A fatal error will occur if
   279  // the service failed to be deleted. This works best when using it as
   280  // a deferred function.
   281  func DeleteService(t *testing.T, client *golangsdk.ServiceClient, serviceID string) {
   282  	err := services.Delete(client, serviceID).ExtractErr()
   283  	if err != nil {
   284  		t.Fatalf("Unable to delete service %s: %v", serviceID, err)
   285  	}
   286  
   287  	t.Logf("Deleted service: %s", serviceID)
   288  }
   289  
   290  // UnassignRole will delete a role assigned to a user/group on a project/domain
   291  // A fatal error will occur if it fails to delete the assignment.
   292  // This works best when using it as a deferred function.
   293  func UnassignRole(t *testing.T, client *golangsdk.ServiceClient, roleID string, opts *roles.UnassignOpts) {
   294  	err := roles.Unassign(client, roleID, *opts).ExtractErr()
   295  	if err != nil {
   296  		t.Fatalf("Unable to unassign a role %v on context %+v: %v", roleID, *opts, err)
   297  	}
   298  	t.Logf("Unassigned the role %v on context %+v", roleID, *opts)
   299  }
   300  
   301  // FindRole finds all roles that the current authenticated client has access
   302  // to and returns the first one found. An error will be returned if the lookup
   303  // was unsuccessful.
   304  func FindRole(t *testing.T, client *golangsdk.ServiceClient) (*roles.Role, error) {
   305  	t.Log("Attempting to find a role")
   306  	var role *roles.Role
   307  
   308  	allPages, err := roles.List(client, nil).AllPages()
   309  	if err != nil {
   310  		return nil, err
   311  	}
   312  
   313  	allRoles, err := roles.ExtractRoles(allPages)
   314  	if err != nil {
   315  		return nil, err
   316  	}
   317  
   318  	for _, r := range allRoles {
   319  		role = &r
   320  		break
   321  	}
   322  
   323  	t.Logf("Successfully found a role %s with ID %s", role.Name, role.ID)
   324  
   325  	return role, nil
   326  }