github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/internal/acceptance/openstack/identity/v3/identity.go (about)

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