conn

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2025 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (

	// AzureCloudConnection
	AzureCloudConnection = AvailableCloudConnectionType{
		Subtype: "Azure",
	}

	// AWSCloudConnection
	AWSCloudConnection = AvailableCloudConnectionType{
		Subtype: "AWS",
	}

	// GCPCloudConnection
	GCPCloudConnection = AvailableCloudConnectionType{
		Subtype: "GCP",
	}

	// AvailableCloudConnectionTypes is a list of available cloud connections.
	AvailableCloudConnectionTypes = []AvailableCloudConnectionType{
		AzureCloudConnection,
		AWSCloudConnection,
		GCPCloudConnection,
	}
)
View Source
var (

	// PostgreSQLDatabaseConnection
	PostgreSQLDatabaseConnection = AvailableDatabaseConnectionType{
		Subtype: "PostgreSQL",
		Driver:  "postgres",
	}

	// MySQLDatabaseConnection
	MySQLDatabaseConnection = AvailableDatabaseConnectionType{
		Subtype: "MySQL",
		Driver:  "mysql",
	}

	// MongoDBDatabaseConnection
	MongoDBDatabaseConnection = AvailableDatabaseConnectionType{
		Subtype: "MongoDB",
		Driver:  "mongodb",
	}

	// AvailableDatabaseConnectionTypes is a list of available database connections.
	AvailableDatabaseConnectionTypes = []AvailableDatabaseConnectionType{
		PostgreSQLDatabaseConnection,
		MySQLDatabaseConnection,
		MongoDBDatabaseConnection,
	}
)
View Source
var (
	ConnectionTypeCloud      = "Cloud"
	ConnectionTypeDatabase   = "Database"
	ConnectionTypeKubernetes = "Kubernetes"
)
View Source
var (
	// AvailableDatabaseConnectionTypes is a list of available database connections.
	AvailableKubernetesConnectionTypes = []AvailableKubernetesConnectionType{}
)
View Source
var TablesAndColumnsQueryMap = map[string]string{
	"postgres": `
        SELECT table_schema, table_name, column_name, data_type
        FROM information_schema.columns
        WHERE table_schema NOT IN ('information_schema', 'pg_catalog')
        ORDER BY table_schema, table_name, ordinal_position;
    `,
	"mysql": `
        SELECT table_schema, table_name, column_name, data_type
        FROM information_schema.columns
        WHERE table_schema NOT IN ('information_schema', 'mysql', 'performance_schema', 'sys')
        ORDER BY table_schema, table_name, ordinal_position;
    `,
}

Functions

func AddQuotesIfNeeded

func AddQuotesIfNeeded(name string) string

AddQuotesIfNeeded adds quotes around the name if it contains capital letters.

func AvailableConnectionTypes

func AvailableConnectionTypes() []string

AvailableConnectionTypes is a list of available connection types.

Types

type AvailableCloudConnectionType

type AvailableCloudConnectionType struct {
	Subtype string
}

AvailableCloudConnectionType is a helper struct to UI to list available cloud connection types. Subtype will be shown in the UI.

type AvailableDatabaseConnectionType

type AvailableDatabaseConnectionType struct {
	Subtype string
	Driver  string
}

AvailableDatabaseConnection is a helper struct to UI to list available database connection types. Subtype will be shown in the UI. Driver will be saved in the connection details.

type AvailableKubernetesConnectionType

type AvailableKubernetesConnectionType struct {
	Subtype string
}

AvailableKubernetesConnection is a helper struct to UI to list available kubernetes connection types. Subtype will be shown in the UI.

type AzureConnection

type AzureConnection struct {
	BaseCloudConnection

	ResourceGroups []AzureResourceGroup
}

func NewAzureConnection

func NewAzureConnection(connnection *Connection) *AzureConnection

func (*AzureConnection) CheckAuthentication

func (a *AzureConnection) CheckAuthentication() error

func (*AzureConnection) CommandType

func (a *AzureConnection) CommandType() string

func (*AzureConnection) GetAnswer

func (a *AzureConnection) GetAnswer(prompt string) (string, error)

func (*AzureConnection) GetCommand

func (a *AzureConnection) GetCommand(prompt string) (string, error)

func (*AzureConnection) GetContext

func (a *AzureConnection) GetContext() string

GetContext returns the resource groups in the Azure connection.

func (*AzureConnection) GetFormattedContext

func (a *AzureConnection) GetFormattedContext() (string, error)

func (*AzureConnection) SetContext

func (a *AzureConnection) SetContext() error

SetContext sets the context for the Azure connection. This will populate the resource groups.

type AzureResourceGroup

type AzureResourceGroup struct {
	Name string `json:"name"`
}

AzureResourceGroup represents an Azure resource group.

type BaseCloudConnection

type BaseCloudConnection struct {
	Connection Connection
}

BaseCloudConnection is a partial implementation of the ConnectionInterface for cloud.

func (*BaseCloudConnection) ExecuteCommand

func (c *BaseCloudConnection) ExecuteCommand(command string) ([]byte, error)

func (*BaseCloudConnection) FormatResultAsTable

func (c *BaseCloudConnection) FormatResultAsTable(result []byte) (string, error)

func (*BaseCloudConnection) GetConnection

func (c *BaseCloudConnection) GetConnection() Connection

type BaseDatabaseConnection

type BaseDatabaseConnection struct {
	Connection Connection
}

BaseDatabaseConnection is a partial implementation of the ConnectionInterface for databases.

func (*BaseDatabaseConnection) GetConnection

func (d *BaseDatabaseConnection) GetConnection() Connection

type BaseRDBMSConnection

type BaseRDBMSConnection struct {
	BaseDatabaseConnection

	// TablesAndColumns is a map of tables and their columns.
	// This will be set via SetContext.
	TablesAndColumns map[string][]ColumnDetail

	// DB is the database connection.
	DB *sql.DB
}

func (*BaseRDBMSConnection) CheckAuthentication

func (b *BaseRDBMSConnection) CheckAuthentication() error

func (*BaseRDBMSConnection) ExecuteCommand

func (b *BaseRDBMSConnection) ExecuteCommand(command string) ([]byte, error)

func (*BaseRDBMSConnection) FormatResultAsTable

func (b *BaseRDBMSConnection) FormatResultAsTable(result []byte) (string, error)

func (*BaseRDBMSConnection) GetContext

func (b *BaseRDBMSConnection) GetContext() string

GetContext returns the tables and columns set by SetContext.

func (*BaseRDBMSConnection) GetFormattedContext

func (b *BaseRDBMSConnection) GetFormattedContext() (string, error)

GetFormattedContext generates a pretty-printed string of the tables and columns.

func (*BaseRDBMSConnection) SetContext

func (b *BaseRDBMSConnection) SetContext() error

SetContext sets the context for the RDBMS connection. It gets the tables and their columns.

type CloudConnectionDetails

type CloudConnectionDetails struct {
}

func GetCloudConnectionDetails

func GetCloudConnectionDetails(conn Connection) (CloudConnectionDetails, error)

GetCloudConnectionDetails retrieves the CloudConnectionDetails from a Connection.

func (CloudConnectionDetails) GetDriver

func (c CloudConnectionDetails) GetDriver() string

type CloudConnectionType

type CloudConnectionType struct {
	// MainType of the connection type.
	// Example: "cloud".
	MainType string `json:"mainType"`

	// Subtype of the cloud connection type.
	// Example: "aws", "gcp", "azure".
	Subtype string `json:"subtype"`
}

func (CloudConnectionType) GetMainType

func (c CloudConnectionType) GetMainType() string

func (CloudConnectionType) GetSubtype

func (c CloudConnectionType) GetSubtype() string

type ColumnDetail

type ColumnDetail struct {
	Name     string
	DataType string
}

ColumnDetail is a helper struct to store the column details. For now, it only stores the name and the data type.

type Connection

type Connection struct {
	// Name of the connection.
	Name string `json:"name"`

	// Type of the connection.
	// Example: "database", "cloud", "kubernetes".
	Type ConnectionType `json:"type"`

	// Details of the connection.
	// Can include different details based on the connection type.
	// Like database connection string, cloud credentials, etc.
	Details ConnectionDetails `json:"details"`
}

func NewCloudConnection

func NewCloudConnection(name string, cloudProvider AvailableCloudConnectionType) Connection

NewCloudConnection creates a new cloud connection.

func NewDatabaseConnection

func NewDatabaseConnection(name string, availableDatabaseConnectionType AvailableDatabaseConnectionType, connectionString string) Connection

NewDatabaseConnection creates a new database connection.

func NewKubernetesConnection

func NewKubernetesConnection(name, context string) Connection

NewKubernetesConnection creates a new Kubernetes connection.

func (*Connection) UnmarshalJSON

func (c *Connection) UnmarshalJSON(data []byte) error

UnmarshalJSON implements custom JSON decoding for the Connection struct.

type ConnectionDetails

type ConnectionDetails interface {
	// GetDriver returns the driver name for the connection.
	// Example: "postgres", "mysql", "mongodb".
	// Can be empty if there is no driver.
	GetDriver() string
}

type ConnectionInterface

type ConnectionInterface interface {
	GetConnection() Connection
	CheckAuthentication() error

	// SetContext gets the necessary information for the connection.
	// For example, for a database connection, it can get the list of tables and columns.
	// For a cloud connection, it can get the list of resources.
	// For a kubernetes connection, it can get the list of deployments, services, etc.
	// This information will be sent to the AI model which will use it to generate the queries/commands.
	SetContext() error

	// GetContext returns the information set by the SetContext method.
	// This information will be sent to the AI model which will use it to generate the queries/commands.
	GetContext() string

	// GetFormattedContext returns the formatted context for the AI model.
	GetFormattedContext() (string, error)

	// ExecuteCommand executes the given command and returns the output as byte array.
	ExecuteCommand(command string) ([]byte, error)

	// FormatResultAsTable formats the result as a table.
	FormatResultAsTable(result []byte) (string, error)

	// GetCommand gets the command from AI using context and the user prompt.
	GetCommand(prompt string) (string, error)

	// GetAnswer gets the answer from AI using context and the user prompt.
	GetAnswer(prompt string) (string, error)

	// CommandType returns the type of the command.
	// Example: "psql", "az", "kubectl".
	CommandType() string
}

func GetConnection

func GetConnection(conn Connection) (ConnectionInterface, error)

Factory function to get the right implementation based on type and subtype

type ConnectionType

type ConnectionType interface {
	// GetMainType returns the main type of the connection.
	// Example: "database", "cloud", "kubernetes".
	GetMainType() string

	// GetSubtype returns the subtype of the connection.
	// Example: "postgres", "mysql", "aws", "gcp", "azure".
	// Can be empty if there is no subtype.
	GetSubtype() string
}

type DatabaseConnectionDetails

type DatabaseConnectionDetails struct {
	// ConnectionString is the connection string for the database.
	ConnectionString string `json:"connectionString"`

	// Driver is the driver name for the database.
	// Example: "postgres", "mysql", "mongodb".
	Driver string `json:"driver"`
}

func GetDatabaseConnectionDetails

func GetDatabaseConnectionDetails(conn Connection) (DatabaseConnectionDetails, error)

GetDatabaseConnectionDetails retrieves the DatabaseConnectionDetails from a Connection.

func (DatabaseConnectionDetails) GetConnectionString

func (d DatabaseConnectionDetails) GetConnectionString() string

func (DatabaseConnectionDetails) GetDriver

func (d DatabaseConnectionDetails) GetDriver() string

type DatabaseConnectionType

type DatabaseConnectionType struct {
	// MainType of the connection type.
	// Example: "database".
	MainType string `json:"mainType"`

	// Subtype is the subtype of the database connection type.
	// Example: "PostgreSQL", "MySQL", "MongoDB".
	Subtype string `json:"subtype"`
}

func (DatabaseConnectionType) GetMainType

func (d DatabaseConnectionType) GetMainType() string

func (DatabaseConnectionType) GetSubtype

func (d DatabaseConnectionType) GetSubtype() string

type Deployment

type Deployment struct {
	Name      string `json:"name"`
	Namespace string `json:"namespace"`
}

Deployment represents a Kubernetes deployment

type KubernetesConnectionDetails

type KubernetesConnectionDetails struct {
	// SelectedContext is the selected context for the kubernetes connection.
	SelectedContext string `json:"selectedContext"`
}

func GetKubernetesConnectionDetails

func GetKubernetesConnectionDetails(conn Connection) (KubernetesConnectionDetails, error)

GetKubernetesConnectionDetails retrieves the KubernetesConnectionDetails from a Connection.

func (KubernetesConnectionDetails) GetDriver

func (k KubernetesConnectionDetails) GetDriver() string

func (KubernetesConnectionDetails) GetSelectedContext

func (k KubernetesConnectionDetails) GetSelectedContext() string

type KubernetesConnectionImpl

type KubernetesConnectionImpl struct {
	Connection Connection

	Namespaces  []Namespace
	Pods        []Pod
	Deployments []Deployment
	Services    []Service
}

KubernetesConnection is the implementation of the ConnectionInterface for Kubernetes.

func NewKubernetesConnectionImpl

func NewKubernetesConnectionImpl(connection *Connection) *KubernetesConnectionImpl

func (*KubernetesConnectionImpl) CheckAuthentication

func (k *KubernetesConnectionImpl) CheckAuthentication() error

func (*KubernetesConnectionImpl) CommandType

func (k *KubernetesConnectionImpl) CommandType() string

func (*KubernetesConnectionImpl) ExecuteCommand

func (k *KubernetesConnectionImpl) ExecuteCommand(command string) ([]byte, error)

func (*KubernetesConnectionImpl) FormatResultAsTable

func (k *KubernetesConnectionImpl) FormatResultAsTable(result []byte) (string, error)

func (*KubernetesConnectionImpl) GetAnswer

func (k *KubernetesConnectionImpl) GetAnswer(prompt string) (string, error)

func (*KubernetesConnectionImpl) GetCommand

func (k *KubernetesConnectionImpl) GetCommand(prompt string) (string, error)

func (*KubernetesConnectionImpl) GetConnection

func (k *KubernetesConnectionImpl) GetConnection() Connection

func (*KubernetesConnectionImpl) GetContext

func (k *KubernetesConnectionImpl) GetContext() string

func (*KubernetesConnectionImpl) GetFormattedContext

func (k *KubernetesConnectionImpl) GetFormattedContext() (string, error)

func (*KubernetesConnectionImpl) SetContext

func (k *KubernetesConnectionImpl) SetContext() error

type KubernetesConnectionType

type KubernetesConnectionType struct {
	// MainType of the connection type.
	// Example: "kubernetes".
	MainType string `json:"mainType"`
}

func (KubernetesConnectionType) GetMainType

func (k KubernetesConnectionType) GetMainType() string

func (KubernetesConnectionType) GetSubtype

func (k KubernetesConnectionType) GetSubtype() string

type Namespace

type Namespace struct {
	Name string `json:"name"`
}

type Pod

type Pod struct {
	Name      string `json:"name"`
	Namespace string `json:"namespace"`
}

Pod represents a Kubernetes pod

type PostgreSQLConnection

type PostgreSQLConnection struct {
	BaseRDBMSConnection
}

func NewPostgreSQLConnection

func NewPostgreSQLConnection(connnection *Connection) *PostgreSQLConnection

func (*PostgreSQLConnection) CommandType

func (p *PostgreSQLConnection) CommandType() string

func (*PostgreSQLConnection) GetAnswer

func (p *PostgreSQLConnection) GetAnswer(prompt string) (string, error)

func (*PostgreSQLConnection) GetCommand

func (p *PostgreSQLConnection) GetCommand(prompt string) (string, error)

type Service

type Service struct {
	Name      string `json:"name"`
	Namespace string `json:"namespace"`
}

Service represents a Kubernetes service

Jump to

Keyboard shortcuts

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