gosdknosqloracle-cloud-infrastructurecloud-object-storage

How to connect an OCI function to OCI NoSQL with Go


I am trying to use the Go SDKs that OCI provides:

  1. OCI SDK for Go
  2. Oracle NoSQL Database Go SDK

I am trying to connect using a config file in the same folder that looks like this:

[DEFAULT]
tenancy=ocid1.tenancy.oc1..abcdefg
user=ocid1.user.oc1..abcdefg
fingerprint=00:00:00:00:00.....
key_file="./oci.pem"
region=eu-frankfurt-1
package main

import (
    "context"
    "encoding/json"
    "io"
    "net/http"

    fdk "github.com/fnproject/fdk-go"
)

func main() {
    fdk.Handle(fdk.HandlerFunc(fnHandler))
}

func fnHandler(ctx context.Context, in io.Reader, out io.Writer) {
    fnctx, ok := fdk.GetContext(ctx).(fdk.HTTPContext)
    if !ok {
        fdk.WriteStatus(out, http.StatusBadRequest)
        io.WriteString(out, `{"error":"Invalid Request"}`)
        return
    }

    /** Oracle NoSQL and Object Storage Connection */

    /**
     *
     *
     *
     *
     *
     *
     *
     */

    response := struct {
        URL     string            `json:"url"`
        Header  http.Header       `json:"header"`
        Config  map[string]string `json:"config"`
        Message bool              `json:"ok"`
    }{
        URL:     fnctx.RequestURL(),
        Header:  fnctx.Header(),
        Config:  fnctx.Config(),
        Message: ok,
    }

    fdk.WriteStatus(out, http.StatusCreated)
    json.NewEncoder(out).Encode(&response)
}

I followed their tutorials and their examples in GiHub.


Solution

  • I found this solution.

    func createClientNoSQL() (*nosqldb.Client, bool) {
        region := "ap-sydney-1"
        compartmentID := "ocid1.compartment.oc1......"
        tenancy := "ocid1.tenancy.oc1......"
        user := "ocid1.user.oc1......"
        fingerprint := "...."
        privateKeyPassphrase := "" // empty or passphrase
        pemString := `-----BEGIN PRIVATE KEY-----
        ......
        -----END PRIVATE KEY-----`
    
        sp, err := iam.NewRawSignatureProvider(tenancy, user, region, fingerprint, compartmentID, pemString, &privateKeyPassphrase)
        if err != nil {
            return nil, false
        }
    
        cfg := nosqldb.Config{
            Mode:                  "cloud",
            Region:                common.Region(region),
            AuthorizationProvider: sp,
        }
    
        lcfg := nosqldb.LoggingConfig{
            Logger: logger.New(os.Stdout, logger.Warn, false),
        }
    
        cfg.LoggingConfig = lcfg
    
        client, err := nosqldb.NewClient(cfg)
        if err != nil {
            return nil, false
        }
    
        return client, true
    }
    

    EDIT:

    The connection with a Resource Principal as mentioned by @Dario is the following:

    sp, err := iam.NewSignatureProviderWithResourcePrincipal("compartment_id")
    if err != nil {
        return
    }
    cfg := nosqldb.Config{
        AuthorizationProvider: sp,
        Region: "<your-service-region>",
    }
    client, err := nosqldb.NewClient(cfg)
    

    Thanks, @Dario, this makes more sense.