gosapcpdestinationclient

package module
v0.0.0-...-63936c6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 12, 2023 License: Apache-2.0 Imports: 5 Imported by: 0

README

Golang client for SAP Cloud Platform Destination Services

GoDoc Go Report Card

Based on the published API at https://api.sap.com/api/SAP_CP_CF_Connectivity_Destination/resource.

This library provides a convenient client for accessing the Destination service on the SAP Cloud Platform Cloud Foundry environments.

Usage

  1. The configuration details required for creating a new DestinationClient instance are provided in the destination service binding in the credentials section. The application must create an instance of the destination service, and bind to it in order to access the configuration details.

    cf create-service destination lite example-destination
    
  2. Add the service to the manifest.yml in order to bind to it.

  3. Configure and use the DestinationClient

package main

import (
	"fmt"
	"os"

	destinations "github.com/liorokman/go-sapcp-destination-client"
	"github.com/tidwall/gjson"
)

func main() {

	vcap := os.Getenv("VCAP_SERVICES")
	if vcap == "" {
		fmt.Printf("No VCAP_SERVICES")
		os.Exit(1)
	}
	destinationClient, err := destinations.NewClient(destinations.DestinationClientConfiguration{
		ClientID:     gjson.Get(vcap, "destination.0.credentials.clientid").String(),
		ClientSecret: gjson.Get(vcap, "destination.0.credentials.clientsecret").String(),
		TokenURL:     gjson.Get(vcap, "destination.0.credentials.url").String(),
		ServiceURL:   gjson.Get(vcap, "destination.0.credentials.uri").String(),
	})
	if err != nil {
		panic(err)
	}
	destinations, err := destinationClient.GetSubaccountDestinations()
	if err != nil {
		panic(err)
	}
	for _, dest := range destinations {
		fmt.Printf("Destination: %s(%s)\n", dest.Name, dest.Type)
	}

}

Documentation

Index

Examples

Constants

View Source
const (
	// Property name for destination Description
	DescriptionProperty = "Description"
	// Property name for the destination Authentication property
	AuthenticationProperty = "Authentication"

	AppToAppSSOAuthentication               = "AppToAppSSO"
	BasicAuthentication                     = "BasicAuthentication"
	ClientCertificateAuthentication         = "ClientCertificateAuthentication"
	NoAuthentication                        = "NoAuthentication"
	OAuth2ClientCredentialsAuthentication   = "OAuth2ClientCredentials"
	OAuth2SAMLBearerAssertionAuthentication = "OAuth2SAMLBearerAssertion"
	OAuth2UserTokenExchangeAuthentication   = "OAuth2UserTokenExchange"
	SAPAssetionSSOAuthentication            = "SAPAssertionSSO"

	// Property name for the destination ProxyType property
	ProxyTypeProperty = "ProxyType"

	// Valid values for the ProxyType property
	InternetProxy  = "Internet"
	OnPremiseProxy = "OnPremise"

	// Property name for the destination URL property
	URLProperty = "URL"

	// Property name for the LocationID destination property
	LocationIDProperty = "LocationID"

	// Property name for the destination User property
	UserProperty = "User"

	// Property name for the destination Password property
	PasswordProperty = "Password"

	// Property name for the destination RepositoryUser property
	RepoUserProperty = "RepositoryUser"

	// Property name for the destination RepositoryPassword property
	RepoPasswordProperty = "RepositoryPassword"
)

Common destination properties

Variables

This section is empty.

Functions

This section is empty.

Types

type AffectedRecords

type AffectedRecords struct {
	Count int `json:"count"`
}

AffectedRecords contains the number of records affected by the operation

type AuthToken

type AuthToken struct {
	// Type of the authentication token
	Type string `json:"type"`
	// Value of the authentication token
	Value string `json:"value"`
}

AuthToken describes an authentication token

type Certificate

type Certificate struct {
	// The name of the destination
	Name string `json:"Name"`
	// The type of the destination
	Type string `json:"Type"`
	// Base64 encoded keystore/certificate binary content
	Content string `json:"Content"`
}

Certificate describes a single certificate

type Destination

type Destination struct {
	// The name of the destination
	Name string
	// The type of the destination
	Type DestinationType
	// Any properties defined on the destination
	Properties map[string]string
}

Destination describes a single Destination

func (Destination) MarshalJSON

func (d Destination) MarshalJSON() ([]byte, error)

MarshalJSON marshalls a Destination object as expected by the Destination RESTful API

func (*Destination) UnmarshalJSON

func (d *Destination) UnmarshalJSON(b []byte) error

UnmarshalJSON unmarshalls a Destination object as provided by the Destination RESTful API

type DestinationClient

type DestinationClient struct {
	// contains filtered or unexported fields
}

DestinationClient provides the client object for accessing destinations in the SAP Cloud Platform Cloud Foundry environment.

func NewClient

func NewClient(clientConf DestinationClientConfiguration) (*DestinationClient, error)

NewClient creates a new DestinationClient object configured according to the provided DestinationClientConfiguration object

Example
client, err := NewClient(DestinationClientConfiguration{
	ClientID:     "clientid",
	ClientSecret: "clientsecret",
	TokenURL:     "https://subdomain.authentication.eu10.hana.ondemand.com",
	ServiceURL:   "https://destination-configuration.cfapps.eu10.hana.ondemand.com",
})
if err != nil {
	panic(err)
}
destinations, err := client.GetSubaccountDestinations()
if err != nil {
	panic(err)
}
fmt.Printf("%#v\n", destinations)
Output:

func (*DestinationClient) CreateInstanceCertificate

func (d *DestinationClient) CreateInstanceCertificate(cert Certificate) error

CreateInstanceCertificate creates a new certificate on the service instance level. The service instance and subaccount are determined by the passed OAuth access token

func (*DestinationClient) CreateInstanceDestination

func (d *DestinationClient) CreateInstanceDestination(newDestination Destination) error

CreateInstanceDestination creates a new destination on the service instance level. The service instance and subaccount are determined by the passed OAuth access token

func (*DestinationClient) CreateSubaccountCertificate

func (d *DestinationClient) CreateSubaccountCertificate(cert Certificate) error

CreateSubaccountCertificate creates a new certificate on the subaccount level. The Subaccount is determined by the passed OAuth access token

func (*DestinationClient) CreateSubaccountDestination

func (d *DestinationClient) CreateSubaccountDestination(newDestination Destination) error

CreateSubaccountDestination creates a new destination on subaccount level. Subaccount is determined by the passed OAuth access token.

func (*DestinationClient) DeleteInstanceCertificate

func (d *DestinationClient) DeleteInstanceCertificate(name string) (AffectedRecords, error)

DeleteInstanceCertificate deletes a certificate posted on the service instance level. The service instance and subaccount are determined by the passed OAuth access token

func (*DestinationClient) DeleteInstanceDestination

func (d *DestinationClient) DeleteInstanceDestination(name string) (AffectedRecords, error)

DeleteInstanceDestination deletes a destination posted on the service instance level. The service instance and subaccount are determined by the passed OAuth access token

func (*DestinationClient) DeleteSubaccountCertificate

func (d *DestinationClient) DeleteSubaccountCertificate(name string) (AffectedRecords, error)

DeleteSubaccountCertificate deletes a certificate posted on the subaccount level. The Subaccount is determined by the passed OAuth access token

func (*DestinationClient) DeleteSubaccountDestination

func (d *DestinationClient) DeleteSubaccountDestination(name string) (AffectedRecords, error)

DeleteSubaccountDestination deletes a destination posted on subaccount level. Subaccount is determined by the passed OAuth access token.

func (*DestinationClient) Find

func (d *DestinationClient) Find(name string, userToken string) (DestinationLookupResult, error)

Find a destination by name on all levels and return the first match. Search priority is destination on service instance level. If none is found, fallbacks to subaccount level (accessible by all apps deployed in the same subaccount). If userToken is not empty, it is passed as the value of the `X-user-token` header. This enables token-exchange flows via the Find operation. If a token-exchange is not required, pass an empty string as the userToken value.

func (*DestinationClient) GetInstanceCertificate

func (d *DestinationClient) GetInstanceCertificate(name string) (Certificate, error)

GetInstanceCertificate retrieves a certificate posted on the service instance level. The service instance and subaccount are determined by the passed OAuth access token

func (*DestinationClient) GetInstanceCertificates

func (d *DestinationClient) GetInstanceCertificates() ([]Certificate, error)

GetInstanceCertificates retrieves all certificates posted on the service instance level. If none are found, an empty list is returned. The service instance and subaccount are determined by the passed OAuth access token

func (*DestinationClient) GetInstanceDestination

func (d *DestinationClient) GetInstanceDestination(name string) (Destination, error)

GetInstanceDestination retrieves a destination posted on the service instance level. The service instance and subaccount are determined by the passed OAuth access token

func (*DestinationClient) GetInstanceDestinations

func (d *DestinationClient) GetInstanceDestinations() ([]Destination, error)

GetInstanceDestinations retrieves all destinations on the service instance level. If none are found, an empty list is returned. Service instance and subaccount are determined the passed OAuth access token

func (*DestinationClient) GetSubaccountCertificate

func (d *DestinationClient) GetSubaccountCertificate(name string) (Certificate, error)

GetSubaccountCertificate retrieves a named certificate posted on the subaccount level. The Subaccount is determined by the passed OAuth access token

func (*DestinationClient) GetSubaccountCertificates

func (d *DestinationClient) GetSubaccountCertificates() ([]Certificate, error)

GetSubaccountCertificates retrieves all certificates posted on the subaccount level. In none are found, an empty array is returned. The Subaccount is determined based on the passed OAuth access token

func (*DestinationClient) GetSubaccountDestination

func (d *DestinationClient) GetSubaccountDestination(name string) (Destination, error)

GetSubaccountDestination retrieves a named destination posted on subaccount level. Subaccount is determined by the passed OAuth access token.

func (*DestinationClient) GetSubaccountDestinations

func (d *DestinationClient) GetSubaccountDestinations() ([]Destination, error)

GetSubaccountDestinations returns a list of destinations posted on subaccount level. If none is found, an empty array is returned. Subaccount is determined by the passed OAuth access token.

func (*DestinationClient) SetDebug

func (d *DestinationClient) SetDebug(debug bool)

SetDebug enables or disables debug output for the DestinationClient

func (*DestinationClient) UpdateInstanceDestination

func (d *DestinationClient) UpdateInstanceDestination(dest Destination) (AffectedRecords, error)

UpdateInstanceDestination updates (overwrites) an existing destination with the passed destination. The service instance and subaccount are determined by the passed OAuth access token

func (*DestinationClient) UpdateSubaccountDestination

func (d *DestinationClient) UpdateSubaccountDestination(dest Destination) (AffectedRecords, error)

UpdateSubaccountDestination updates (overwrites) an existing destination with a new destination, posted on subaccount level. Subaccount is determined by the passed OAuth access token

type DestinationClientConfiguration

type DestinationClientConfiguration struct {
	// ClientID for authentication purposes. Use the clientid attribute in the service binding
	ClientID string
	// ClientSecret for authentication purposes. Use the clientsecret attribute in the service binding
	ClientSecret string
	// TokenURL for authentication purposes. Use the url attribute in the service binding
	TokenURL string
	// ServiceURL for accessing the service RESTful endpoint. Use the uri attribute in the service binding
	ServiceURL string
}

DestinationClientConfiguration contains the values required for configuring a new Destination client

type DestinationFinder

type DestinationFinder interface {
	Find(name string) (DestinationLookupResult, error)
}

DestinationFinder provides a Find method for discovering destinations on any level.

type DestinationLookupResult

type DestinationLookupResult struct {
	// The level on which the destination is defined
	Owner Owner `json:"owner,omitempty"`
	// The destination information
	Destination Destination `json:"destinationConfiguration,omitempty"`
	// Certificates (if present) for the destination
	Certificates []Certificate `json:"certificates,omitempty"`
	// Authentication tokens (if present) for the destination
	AuthTokens []AuthToken `json:"authTokens,omitempty"`
}

DestinationLookupResult contains the result of a find operation

type DestinationType

type DestinationType string

DestinationType enumeration

const (
	// HTTPDestination type
	HTTPDestination DestinationType = "HTTP"
	// RFCDestination type
	RFCDestination DestinationType = "RFC"
	// MailDestination type
	MailDestination DestinationType = "MAIL"
	// LDAPDestination type
	LDAPDestination DestinationType = "LDAP"
)

type ErrorMessage

type ErrorMessage struct {
	ErrorMessage string `json:"ErrorMessage"`
	// contains filtered or unexported fields
}

ErrorMessage struct contains errors returned by the Destination API

func (ErrorMessage) Error

func (e ErrorMessage) Error() string

func (ErrorMessage) StatusCode

func (e ErrorMessage) StatusCode() int

StatusCode returns the status code provided with the error

type InstanceCertificateManager

type InstanceCertificateManager interface {
	GetInstanceCertificates() ([]Certificate, error)
	CreateInstanceCertificate(cert Certificate) error
	GetInstanceCertificate(name string) (Certificate, error)
	DeleteInstanceCertificate(name string) (AffectedRecords, error)
}

InstanceCertificateManager provides an interface for methods that manage certificates on the Instance level

type InstanceDestinationManager

type InstanceDestinationManager interface {
	GetInstanceDestinations() ([]Destination, error)
	CreateInstanceDestination(newDestination Destination) error
	UpdateInstanceDestination(dest Destination) (AffectedRecords, error)
	GetInstanceDestination(name string) (Destination, error)
	DeleteInstanceDestination(name string) (AffectedRecords, error)
}

InstanceDestinationManager provides an interface for methods that manage destinations on the Instance level

type Owner

type Owner struct {
	// Subaccount ID owning this destination
	SubaccountID string `json:"SubaccountId,omitempty"`
	// Instance ID owning this destination
	InstanceID string `json:"InstanceId,omitempty"`
}

Owner describes the level on which the destination is defined. At least one of SubaccountID or InstanceID are guaranteed to have a value.

type SubaccountCertificateManager

type SubaccountCertificateManager interface {
	GetSubaccountCertificates() ([]Certificate, error)
	CreateSubaccountCertificate(cert Certificate) error
	GetSubaccountCertificate(name string) (Certificate, error)
	DeleteSubaccountCertificate(name string) (AffectedRecords, error)
}

SubaccountCertificateManager provides an interface for methods that manage certificates on the Subaccount level

type SubaccountDestinationManager

type SubaccountDestinationManager interface {
	GetSubaccountDestinations() ([]Destination, error)
	CreateSubaccountDestination(newDestination Destination) error
	UpdateSubaccountDestination(dest Destination) (AffectedRecords, error)
	GetSubaccountDestination(name string) (Destination, error)
	DeleteSubaccountDestination(name string) (AffectedRecords, error)
}

SubaccountDestinationManager provides an interface for methods that manage destinations on the Subaccount level

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL