github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/identity/federatedauth/protocols/requests.go (about)

     1  package protocols
     2  
     3  import "github.com/chnsz/golangsdk"
     4  
     5  var RequestOpts = golangsdk.RequestOpts{
     6  	MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
     7  }
     8  
     9  type ProtocolOption struct {
    10  	MappingID string `json:"mapping_id,omitempty"`
    11  }
    12  
    13  func Create(c *golangsdk.ServiceClient, idpID, protocolID string, mappingID string) (*IdentityProtocol, error) {
    14  	opts := ProtocolOption{MappingID: mappingID}
    15  	b, err := golangsdk.BuildRequestBody(opts, "protocol")
    16  	if err != nil {
    17  		return nil, err
    18  	}
    19  
    20  	var rst golangsdk.Result
    21  	_, err = c.Put(resourceURL(c, idpID, protocolID), b, &rst.Body, &golangsdk.RequestOpts{
    22  		MoreHeaders: RequestOpts.MoreHeaders,
    23  	})
    24  	if err == nil {
    25  		var r struct {
    26  			Protocol IdentityProtocol `json:"protocol"`
    27  		}
    28  		rst.ExtractInto(&r)
    29  		return &r.Protocol, nil
    30  	}
    31  	return nil, err
    32  }
    33  
    34  func Get(c *golangsdk.ServiceClient, idpID string, protocolID string) (*IdentityProtocol, error) {
    35  	var rst golangsdk.Result
    36  	_, err := c.Get(resourceURL(c, idpID, protocolID), &rst.Body, &golangsdk.RequestOpts{
    37  		MoreHeaders: RequestOpts.MoreHeaders,
    38  	})
    39  
    40  	if err == nil {
    41  		var r struct {
    42  			Protocol IdentityProtocol `json:"protocol"`
    43  		}
    44  		rst.ExtractInto(&r)
    45  		return &r.Protocol, nil
    46  	}
    47  	return nil, err
    48  }
    49  
    50  func List(c *golangsdk.ServiceClient, idpID string) ([]IdentityProtocol, error) {
    51  	var rst golangsdk.Result
    52  	_, err := c.Get(root(c, idpID), &rst.Body, &golangsdk.RequestOpts{
    53  		MoreHeaders: RequestOpts.MoreHeaders,
    54  	})
    55  
    56  	if err == nil {
    57  		var r struct {
    58  			Protocols []IdentityProtocol `json:"protocols"`
    59  		}
    60  		rst.ExtractInto(&r)
    61  		return r.Protocols, nil
    62  	}
    63  	return nil, err
    64  }
    65  
    66  func Update(c *golangsdk.ServiceClient, idpID string, id string, mappingID string) (*IdentityProtocol, error) {
    67  	opts := ProtocolOption{MappingID: mappingID}
    68  	b, err := golangsdk.BuildRequestBody(opts, "protocol")
    69  	if err != nil {
    70  		return nil, err
    71  	}
    72  
    73  	var rst golangsdk.Result
    74  	_, err = c.Patch(resourceURL(c, idpID, id), b, &rst.Body, &golangsdk.RequestOpts{
    75  		MoreHeaders: RequestOpts.MoreHeaders,
    76  	})
    77  	if err == nil {
    78  		var r struct {
    79  			Protocol IdentityProtocol `json:"protocol"`
    80  		}
    81  		rst.ExtractInto(&r)
    82  		return &r.Protocol, nil
    83  	}
    84  	return nil, err
    85  }
    86  
    87  func Delete(c *golangsdk.ServiceClient, idpID string, protocolID string) *golangsdk.ErrResult {
    88  	var r golangsdk.ErrResult
    89  	_, r.Err = c.Delete(resourceURL(c, idpID, protocolID), &golangsdk.RequestOpts{
    90  		MoreHeaders: RequestOpts.MoreHeaders,
    91  	})
    92  	return &r
    93  }