Simplify a few acceptance tests for illustration.
Expect a big-bang PR following this one. These two changes illustrate
how to use the newly refactored code.
diff --git a/acceptance/02-list-servers.go b/acceptance/02-list-servers.go
index 758c706..df9e28c 100644
--- a/acceptance/02-list-servers.go
+++ b/acceptance/02-list-servers.go
@@ -9,32 +9,14 @@
var quiet = flag.Bool("quiet", false, "Quiet mode, for acceptance testing. $? still indicates errors though.")
func main() {
- provider, username, password := getCredentials()
flag.Parse()
- acc, err := gophercloud.Authenticate(
- provider,
- gophercloud.AuthOptions{
- Username: username,
- Password: password,
- },
- )
- if err != nil {
- panic(err)
- }
-
- api, err := gophercloud.ServersApi(acc, gophercloud.ApiCriteria{
- Name: "cloudServersOpenStack",
- Region: "DFW",
- VersionId: "2",
- UrlChoice: gophercloud.PublicURL,
+ withIdentity(func(acc gophercloud.AccessProvider) {
+ withServerApi(acc, func(api gophercloud.CloudServersProvider) {
+ tryFullDetails(api)
+ tryLinksOnly(api)
+ })
})
- if err != nil {
- panic(err)
- }
-
- tryFullDetails(api)
- tryLinksOnly(api)
}
func tryLinksOnly(api gophercloud.CloudServersProvider) {
diff --git a/acceptance/03-get-server-details.go b/acceptance/03-get-server-details.go
index bae220f..3fda268 100644
--- a/acceptance/03-get-server-details.go
+++ b/acceptance/03-get-server-details.go
@@ -11,85 +11,67 @@
var quiet = flag.Bool("quiet", false, "Run quietly, for acceptance testing. $? non-zero if issue.")
func main() {
- provider, username, password := getCredentials()
flag.Parse()
- auth, err := gophercloud.Authenticate(
- provider,
- gophercloud.AuthOptions{
- Username: username,
- Password: password,
- },
- )
- if err != nil {
- panic(err)
- }
+ withIdentity(func(auth gophercloud.AccessProvider) {
+ withServerApi(auth, func(servers gophercloud.CloudServersProvider) {
+ serverId := ""
+ if *id == "" {
+ ss, err := servers.ListServers()
+ if err != nil {
+ panic(err)
+ }
+ // We could just cheat and dump the server details from ss[0].
+ // But, that tests ListServers(), and not ServerById(). So, we
+ // elect not to cheat.
+ serverId = ss[0].Id
+ } else {
+ serverId = *id
+ }
- servers, err := gophercloud.ServersApi(auth, gophercloud.ApiCriteria{
- Name: "cloudServersOpenStack",
- Region: *rgn,
- VersionId: "2",
- UrlChoice: gophercloud.PublicURL,
+ s, err := servers.ServerById(serverId)
+ if err != nil {
+ panic(err)
+ }
+
+ configs := []string {
+ "Access IPv4: %s\n",
+ "Access IPv6: %s\n",
+ " Created: %s\n",
+ " Flavor: %s\n",
+ " Host ID: %s\n",
+ " ID: %s\n",
+ " Image: %s\n",
+ " Name: %s\n",
+ " Progress: %s\n",
+ " Status: %s\n",
+ " Tenant ID: %s\n",
+ " Updated: %s\n",
+ " User ID: %s\n",
+ }
+
+ values := []string {
+ s.AccessIPv4,
+ s.AccessIPv6,
+ s.Created,
+ s.Flavor.Id,
+ s.HostId,
+ s.Id,
+ s.Image.Id,
+ s.Name,
+ fmt.Sprintf("%d", s.Progress),
+ s.Status,
+ s.TenantId,
+ s.Updated,
+ s.UserId,
+ }
+
+ if !*quiet {
+ fmt.Println("Server info:")
+ for i, _ := range configs {
+ fmt.Printf(configs[i], values[i])
+ }
+ }
+ })
})
- if err != nil {
- panic(err)
- }
-
- serverId := ""
- if *id == "" {
- ss, err := servers.ListServers()
- if err != nil {
- panic(err)
- }
- // We could just cheat and dump the server details from ss[0].
- // But, that tests ListServers(), and not ServerById(). So, we
- // elect not to cheat.
- serverId = ss[0].Id
- } else {
- serverId = *id
- }
-
- s, err := servers.ServerById(serverId)
- if err != nil {
- panic(err)
- }
-
- configs := []string {
- "Access IPv4: %s\n",
- "Access IPv6: %s\n",
- " Created: %s\n",
- " Flavor: %s\n",
- " Host ID: %s\n",
- " ID: %s\n",
- " Image: %s\n",
- " Name: %s\n",
- " Progress: %s\n",
- " Status: %s\n",
- " Tenant ID: %s\n",
- " Updated: %s\n",
- " User ID: %s\n",
- }
-
- values := []string {
- s.AccessIPv4,
- s.AccessIPv6,
- s.Created,
- s.Flavor.Id,
- s.HostId,
- s.Id,
- s.Image.Id,
- s.Name,
- fmt.Sprintf("%d", s.Progress),
- s.Status,
- s.TenantId,
- s.Updated,
- s.UserId,
- }
-
- if !*quiet {
- fmt.Println("Server info:")
- for i, _ := range configs {
- fmt.Printf(configs[i], values[i])
- }
- }
}