...

Source file src/github.com/LINBIT/golinstor/client/example_test.go

Documentation: github.com/LINBIT/golinstor/client

     1  package client_test
     2  
     3  import (
     4  	"context"
     5  	"crypto/tls"
     6  	"fmt"
     7  	"net/http"
     8  	"net/url"
     9  
    10  	"github.com/LINBIT/golinstor/client"
    11  	log "github.com/sirupsen/logrus"
    12  )
    13  
    14  func Example_simple() {
    15  	ctx := context.TODO()
    16  
    17  	u, err := url.Parse("http://controller:3370")
    18  	if err != nil {
    19  		log.Fatal(err)
    20  	}
    21  
    22  	c, err := client.NewClient(client.BaseURL(u), client.Log(log.StandardLogger()))
    23  	if err != nil {
    24  		log.Fatal(err)
    25  	}
    26  
    27  	rs, err := c.Resources.GetAll(ctx, "foo")
    28  	if err != nil {
    29  		log.Fatal(err)
    30  	}
    31  
    32  	for _, r := range rs {
    33  		fmt.Printf("Resource with name '%s' on node with name '%s'\n", r.Name, r.NodeName)
    34  	}
    35  }
    36  
    37  func Example_https() {
    38  	ctx := context.TODO()
    39  
    40  	u, err := url.Parse("https://controller:3371")
    41  	if err != nil {
    42  		log.Fatal(err)
    43  	}
    44  
    45  	// Be careful if that is really what you want!
    46  	trSkipVerify := &http.Transport{
    47  		TLSClientConfig: &tls.Config{
    48  			InsecureSkipVerify: true,
    49  		},
    50  	}
    51  	httpClient := &http.Client{
    52  		Transport: trSkipVerify,
    53  	}
    54  
    55  	c, err := client.NewClient(client.BaseURL(u), client.HTTPClient(httpClient))
    56  	if err != nil {
    57  		log.Fatal(err)
    58  	}
    59  
    60  	rs, err := c.Resources.GetAll(ctx, "foo")
    61  	if err != nil {
    62  		log.Fatal(err)
    63  	}
    64  
    65  	for _, r := range rs {
    66  		fmt.Printf("Resource with name '%s' on node with name '%s'\n", r.Name, r.NodeName)
    67  	}
    68  }
    69  
    70  func Example_httpsauth() {
    71  	ctx := context.TODO()
    72  
    73  	u, err := url.Parse("https://controller:3371")
    74  	if err != nil {
    75  		log.Fatal(err)
    76  	}
    77  
    78  	// Be careful if that is really what you want!
    79  	trSkipVerify := &http.Transport{
    80  		TLSClientConfig: &tls.Config{
    81  			InsecureSkipVerify: true,
    82  		},
    83  	}
    84  	httpClient := &http.Client{
    85  		Transport: trSkipVerify,
    86  	}
    87  
    88  	c, err := client.NewClient(client.BaseURL(u), client.HTTPClient(httpClient),
    89  		client.BasicAuth(&client.BasicAuthCfg{Username: "Username", Password: "Password"}))
    90  	if err != nil {
    91  		log.Fatal(err)
    92  	}
    93  
    94  	rs, err := c.Resources.GetAll(ctx, "foo")
    95  	if err != nil {
    96  		log.Fatal(err)
    97  	}
    98  
    99  	for _, r := range rs {
   100  		fmt.Printf("Resource with name '%s' on node with name '%s'\n", r.Name, r.NodeName)
   101  	}
   102  }
   103  
   104  func Example_error() {
   105  	ctx := context.TODO()
   106  
   107  	u, err := url.Parse("http://controller:3370")
   108  	if err != nil {
   109  		log.Fatal(err)
   110  	}
   111  
   112  	c, err := client.NewClient(client.BaseURL(u), client.Log(log.StandardLogger()))
   113  	if err != nil {
   114  		log.Fatal(err)
   115  	}
   116  
   117  	rs, err := c.Resources.GetAll(ctx, "foo")
   118  	if errs, ok := err.(client.ApiCallError); ok {
   119  		log.Error("A LINSTOR API error occurred:")
   120  		for i, e := range errs {
   121  			log.Errorf("  Message #%d:", i)
   122  			log.Errorf("    Code: %d", e.RetCode)
   123  			log.Errorf("    Message: %s", e.Message)
   124  			log.Errorf("    Cause: %s", e.Cause)
   125  			log.Errorf("    Details: %s", e.Details)
   126  			log.Errorf("    Correction: %s", e.Correction)
   127  			log.Errorf("    Error Reports: %v", e.ErrorReportIds)
   128  		}
   129  		return
   130  	}
   131  	if err != nil {
   132  		log.Fatalf("Some other error occurred: %s", err.Error())
   133  	}
   134  
   135  	for _, r := range rs {
   136  		fmt.Printf("Resource with name '%s' on node with name '%s'\n", r.Name, r.NodeName)
   137  	}
   138  }
   139  
   140  func Example_events() {
   141  	ctx := context.TODO()
   142  
   143  	u, err := url.Parse("http://controller:3370")
   144  	if err != nil {
   145  		log.Fatal(err)
   146  	}
   147  
   148  	c, err := client.NewClient(client.BaseURL(u))
   149  	if err != nil {
   150  		log.Fatal(err)
   151  	}
   152  
   153  	mayPromoteStream, err := c.Events.DRBDPromotion(ctx, "")
   154  	if err != nil {
   155  		log.Fatal(err)
   156  	}
   157  	defer mayPromoteStream.Close()
   158  
   159  	for ev := range mayPromoteStream.Events {
   160  		fmt.Printf("Resource '%s' on node with name '%s' may promote: %t\n", ev.ResourceName, ev.NodeName, ev.MayPromote)
   161  	}
   162  }
   163  
   164  func Example_multipleControllers() {
   165  	ctx := context.TODO()
   166  	controllers := []string{"alfa:3370", "bravo:3370", "charlie:3370"}
   167  
   168  	c, err := client.NewClient(client.Controllers(controllers))
   169  	if err != nil {
   170  		log.Fatal(err)
   171  	}
   172  
   173  	// This call will try each of the controllers and use the first one that responds
   174  	version, err := c.Controller.GetVersion(ctx)
   175  	if err != nil {
   176  		log.Fatal(err)
   177  	}
   178  	fmt.Printf("Controller Version: %v", version)
   179  }
   180  

View as plain text