Coding Applications for Astra in GO - datastaxdevs/awesome-astra GitHub Wiki

🏠 Back to home | Written by Cedrick Lunven, Last Update 3/11/2022

Working with Go

Astra provides include multiple components, and, for each, there are multiple interfaces as shown on the schema on the right. Frameworks and tools and methods to integrate with Astra are related to the interface used.

Pick the interface in the table to get relevant instructions. In most case you download a working sample. Those are standalone examples designed as simple as possible.

More to come soon. Stay Tuned.

Pick the interface you need to use

Component Interface Description
Astra DB cql Main connection to Cassandra
Astra DB cql CQL exposes as stateless rest resources
Astra DB cql Use Cassandra as a Document DB
Astra DB cql Create tables and use generated CRUD
Astra DB cql CQL exposes through serialized protobuf
Astra Streaming cql Create Producer, Consumers, Subscriptions..
Astra Streaming cql Administrate your Pulsar cluster
Astra Core cql Manage users and roles
Astra Core cql Manage Databases
Astra Core cql Manage Streaming

CQL Cassandra Drivers

ℹ️ Overview

TODO

📦. Prerequisites [ASTRA]

TODO

📦. Prerequisites [Development Environment]

TODO

📦. Setup Project

TODO

🖥️. Sample Code

package main

import (
	"archive/zip"
	"context"
	"crypto/tls"
	"crypto/x509"
	"encoding/json"
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"path/filepath"
	"strconv"
	"strings"

	"github.com/gocql/gocql"
)

type Config struct {
	Host string `json:"host"`
	Port int    `json:"cql_port"`
}

func main() {
	var clientID = os.Getenv("ASTRA_CLIENT_ID")
	var clientSecret = os.Getenv("ASTRA_CLIENT_SECRET")
	var secureConnectBundle = os.Getenv("SECURE_CONNECT_BUNDLE")
	if clientID == "" || clientSecret == "" || secureConnectBundle == "" {
		panic("missing required environment variables")
	}

	secureBundleDir := os.TempDir()
	fmt.Printf("extracting secure connect bundle [%s] to [%s]\n", secureConnectBundle, secureBundleDir)
	if err := Unzip(secureConnectBundle, secureBundleDir); err != nil {
		panic(err)
	}

	configPath, _ := filepath.Abs(secureBundleDir + "/config.json")
	fmt.Println("config: " + configPath)
	configData, _ := ioutil.ReadFile(configPath)
	var cfg Config
	json.Unmarshal(configData, &cfg)

	cluster := gocql.NewCluster(cfg.Host)
	cluster.Authenticator = gocql.PasswordAuthenticator{
		Username: clientID,
		Password: clientSecret,
	}
	host := cfg.Host + ":" + strconv.Itoa(cfg.Port)
	cluster.Hosts = []string{host}
	fmt.Println("connecting to: " + host)

	certPath, _ := filepath.Abs(secureBundleDir + "/cert")
	keyPath, _ := filepath.Abs(secureBundleDir + "/key")
	caPath, _ := filepath.Abs(secureBundleDir + "/ca.crt")
	cert, _ := tls.LoadX509KeyPair(certPath, keyPath)

	caCert, _ := ioutil.ReadFile(caPath)
	caCertPool := x509.NewCertPool()
	caCertPool.AppendCertsFromPEM(caCert)

	cluster.SslOpts = &gocql.SslOptions{
		Config: &tls.Config{
			Certificates: []tls.Certificate{cert},
			ServerName:   cfg.Host,
			RootCAs:      caCertPool,
		},
	}

	session, err := cluster.CreateSession()
	if err != nil {
		panic(err)
	}

	fmt.Printf("session established: %v\n", session)

	var releaseVersion string
	if err := session.Query("select release_version from system.local").
		WithContext(context.Background()).
		Consistency(gocql.One).
		Scan(&releaseVersion); err != nil {
		panic(err)
	}

	fmt.Printf("release version: %s\n", releaseVersion)
}

func Unzip(src, dest string) error {
	r, err := zip.OpenReader(src)
	if err != nil {
		return err
	}
	defer func() {
		if err := r.Close(); err != nil {
			panic(err)
		}
	}()

	os.MkdirAll(dest, 0755)

	// Closure to address file descriptors issue with all the deferred .Close() methods
	extractAndWriteFile := func(f *zip.File) error {
		rc, err := f.Open()
		if err != nil {
			return err
		}
		defer func() {
			if err := rc.Close(); err != nil {
				panic(err)
			}
		}()

		path := filepath.Join(dest, f.Name)

		// Check for ZipSlip (Directory traversal)
		if !strings.HasPrefix(path, filepath.Clean(dest)+string(os.PathSeparator)) {
			return fmt.Errorf("illegal file path: %s", path)
		}

		if f.FileInfo().IsDir() {
			os.MkdirAll(path, f.Mode())
		} else {
			os.MkdirAll(filepath.Dir(path), f.Mode())
			f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
			if err != nil {
				return err
			}
			defer func() {
				if err := f.Close(); err != nil {
					panic(err)
				}
			}()

			_, err = io.Copy(f, rc)
			if err != nil {
				return err
			}
		}
		return nil
	}

	for _, f := range r.File {
		err := extractAndWriteFile(f)
		if err != nil {
			return err
		}
	}

	return nil
}

⏫ Back to top

Stargate REST Api

ℹ️ Overview

TODO

📦. Prerequisites [ASTRA]

TODO

📦. Prerequisites [Development Environment]

TODO

📦. Setup Project

TODO

🖥️. Sample Code

TODO

⏫ Back to top

Stargate Document Api

ℹ️ Overview

TODO

📦. Prerequisites [ASTRA]

TODO

📦. Prerequisites [Development Environment]

TODO

📦. Setup Project

TODO

🖥️. Sample Code

TODO

⏫ Back to top

Stargate GraphQL

5.1 Cql First

ℹ️ Overview

TODO

📦. Prerequisites [ASTRA]

TODO

📦. Prerequisites [Development Environment]

TODO

📦. Setup Project

TODO

🖥️. Sample Code

TODO

⏫ Back to top

5.2 GraphQl Types First

ℹ️ Overview

TODO

📦. Prerequisites [ASTRA]

TODO

📦. Prerequisites [Development Environment]

TODO

📦. Setup Project

TODO

🖥️. Sample Code

TODO

⏫ Back to top

Stargate gRPC

ℹ️ Overview

TODO

📦. Prerequisites [ASTRA]

TODO

📦. Prerequisites [Development Environment]

TODO

📦. Setup Project

TODO

🖥️. Sample Code

TODO

⏫ Back to top

Pulsar Client

ℹ️ Overview

TODO

📦. Prerequisites [ASTRA]

TODO

📦. Prerequisites [Development Environment]

TODO

📦. Setup Project

TODO

🖥️. Sample Code

TODO

⏫ Back to top

Pulsar Admin

ℹ️ Overview

TODO

📦. Prerequisites [ASTRA]

TODO

📦. Prerequisites [Development Environment]

TODO

📦. Setup Project

TODO

🖥️. Sample Code

TODO

⏫ Back to top

Devops API

Devops Database API

ℹ️ Overview

TODO

📦. Prerequisites [ASTRA]

TODO

📦. Prerequisites [Development Environment]

TODO

📦. Setup Project

TODO

🖥️. Sample Code

TODO

⏫ Back to top

Devops Streaming API

ℹ️ Overview

TODO

📦. Prerequisites [ASTRA]

TODO

📦. Prerequisites [Development Environment]

TODO

📦. Setup Project

TODO

🖥️. Sample Code

TODO

⏫ Back to top

Devops Organization Apis

ℹ️ Overview

TODO

📦. Prerequisites [ASTRA]

TODO

📦. Prerequisites [Development Environment]

TODO

📦. Setup Project

TODO

🖥️. Sample Code

TODO

⏫ Back to top