pingdirectory-go-client (Go)
The pingdirectory-go-client SDK has not yet been released to version 1.0.0. While general use of the SDK is not expected to change and is stable, later versions could contain minor changes to package and function names.
|
Use the PingDirectory Go SDK to connect your Go application to your PingDirectory deployment.
The pingdirectory-go-client module provides API bindings to the PingDirectory configuration API, allowing developers to invoke API services simply using strongly typed request and response payloads. The module can be included as a dependency in developer code.
Prerequisites
The pingdirectory-go-client
module requires a running PingDirectory instance. The module requires the use of the PingDirectory Configuration API, which must be enabled on the service and should be configured to run on the HTTPS Connection Handler.
Learn more about the PingDirectory Configuration API in Using the Configuration API in the PingDirectory documentation.
Getting started
The following example shows how to include the pingdirectory-go-client
module in a developer project.
If you haven’t done so already, initialize a new Go module project:
go mod init github.com/mygithubuser/my-go-project
To determine the version of the pingdirectory-go-client
to use, take the full PingDirectory server version and omit the version separator.
For example, for a client for PingDirectory version 10.2.0.0, the version of the Go client to use will be v10200.
Use the standard Go commands to install pingdirectory-go-client
to the project for your version of PingDirectory:
go get github.com/pingidentity/pingdirectory-go-client/v10200
Connect to the service
The following sections describe the available options to connect the configuration management SDK to the PingDirectory service.
HTTP basic authentication
When connecting to the PingDirectory service, you can use HTTP basic authentication, as in the following example:
package main
import (
"context"
"crypto/tls"
"fmt"
"net/http"
pingdirectory "github.com/pingidentity/pingdirectory-go-client/v10200/configurationapi"
)
func main() {
pdHttpHost := "https://localhost:1443"
pdUsername := "cn=administrator"
pdPassword := "my-admin-password"
// Initialize the API client
clientConfig := pingdirectory.NewConfiguration()
clientConfig.Servers = pingdirectory.ServerConfigurations{
{
URL: pdHttpHost + "/config",
},
}
tr := &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: false,
},
}
clientConfig.HTTPClient = &http.Client{Transport: tr}
apiClient := pingdirectory.NewAPIClient(clientConfig)
// Set Basic Auth credentials in the Go context
basicAuthContext := context.WithValue(context.Background(), pingdirectory.ContextBasicAuth, pingdirectory.BasicAuth{
UserName: pdUsername,
Password: pdPassword,
})
// Call an API from the `apiClient` object
readResponse, httpResponse, err := apiClient.RootDnAPI.GetRootDn(basicAuthContext).Execute()
if err != nil {
panic(err)
}
fmt.Printf("Response HTTP Code: %d", httpResponse.StatusCode)
fmt.Printf("Response: %v", readResponse)
}