github.com/gophercloud/gophercloud@v1.11.0/openstack/baremetal/httpbasic/requests.go (about) 1 package httpbasic 2 3 import ( 4 "encoding/base64" 5 "fmt" 6 7 "github.com/gophercloud/gophercloud" 8 ) 9 10 // EndpointOpts specifies a "http_basic" Ironic Endpoint 11 type EndpointOpts struct { 12 IronicEndpoint string 13 IronicUser string 14 IronicUserPassword string 15 } 16 17 func initClientOpts(client *gophercloud.ProviderClient, eo EndpointOpts) (*gophercloud.ServiceClient, error) { 18 sc := new(gophercloud.ServiceClient) 19 if eo.IronicEndpoint == "" { 20 return nil, fmt.Errorf("IronicEndpoint is required") 21 } 22 if eo.IronicUser == "" || eo.IronicUserPassword == "" { 23 return nil, fmt.Errorf("User and Password are required") 24 } 25 26 token := []byte(eo.IronicUser + ":" + eo.IronicUserPassword) 27 encodedToken := base64.StdEncoding.EncodeToString(token) 28 sc.MoreHeaders = map[string]string{"Authorization": "Basic " + encodedToken} 29 sc.Endpoint = gophercloud.NormalizeURL(eo.IronicEndpoint) 30 sc.ProviderClient = client 31 return sc, nil 32 } 33 34 // NewBareMetalHTTPBasic creates a ServiceClient that may be used to access a 35 // "http_basic" bare metal service. 36 func NewBareMetalHTTPBasic(eo EndpointOpts) (*gophercloud.ServiceClient, error) { 37 sc, err := initClientOpts(&gophercloud.ProviderClient{}, eo) 38 if err != nil { 39 return nil, err 40 } 41 42 sc.Type = "baremetal" 43 44 return sc, nil 45 }