Trying struct literals and other methods of value setting.

data-work
Mike Holloway 4 months ago
parent 84155e8590
commit 2f56639f5b
  1. 23
      anvildb.go
  2. 60
      main.go
  3. 1
      methods.go
  4. 14
      models.go
  5. 49
      types.go

@ -1,24 +1,3 @@
package main
import (
"database/sql"
"github.com/uptrace/bun"
"github.com/uptrace/bun/dialect/pgdialect"
"github.com/uptrace/bun/driver/pgdriver"
"github.com/uptrace/bun/extra/bundebug"
)
func ConnectAnvilDB(dsn string) *bun.DB {
sqldb := sql.OpenDB(pgdriver.NewConnector(pgdriver.WithDSN(dsn)))
db := bun.NewDB(sqldb, pgdialect.New())
db.AddQueryHook(bundebug.NewQueryHook(
bundebug.WithVerbose(true),
bundebug.FromEnv("BUNDEBUG"),
))
return db
}
//func GetFeeds(db *bun.DB)
//anvildb.NewSelect().Model(&SSHKey{}).OrderExpr("ssh_key_user_name ASC").

@ -2,12 +2,17 @@ package main
import (
"context"
"errors"
"database/sql"
"github.com/uptrace/bun/driver/pgdriver"
)
var users []User
var ssh_keys []SSHKey
// Tofu Provider is the goal
// Data Sources first
// Resources later
//var users []UserModel
//var ssh_keys []SSHKeyModel
//var servers []Server
//var server_definitions []ServerDefinition
//var manifests []Manifest
@ -15,27 +20,38 @@ var ssh_keys []SSHKey
//var hosts []Host
func main() {
var err error
ctx := context.Background()
dsn := "postgres://postgres:Initial1@localhost:15432/anvil?sslmode=disable"
// dsn := "unix://user:pass@dbname/var/run/postgresql/.s.PGSQL.5432"
var (
err error
ctx = context.Background()
dsn = "postgres://postgres:Initial1@localhost:15432/anvil?sslmode=disable"
// dsn := "unix://user:pass@dbname/var/run/postgresql/.s.PGSQL.5432"
)
anvildb := ConnectAnvilDB(dsn)
// Turn these into funcs with error handling, logging, return vals etc.
if usererr := anvildb.NewSelect().
// Turning *User into an interface &User{}
Model(&User{}).
OrderExpr("user_uuid ASC").
Limit(10).
Scan(ctx, &users); usererr != nil {
errors.Join(usererr, err)
}
if sshkeyerr := anvildb.NewSelect().
Model(&SSHKey{}).
OrderExpr("ssh_key_user_name ASC").
Scan(ctx, &ssh_keys); sshkeyerr != nil {
errors.Join(sshkeyerr, err)
sqldb := sql.OpenDB(pgdriver.NewConnector(pgdriver.WithDSN(dsn)))
anvil := &Anvil{
Strikers: []Striker{
Striker{
FQDN: "mh-striker01.libre.audio",
},
},
}
anvil.Strikers[0].Endpoints.Postgresql = &sqldb
// Turn these into funcs with error handling, logging, return vals etc.
// if usererr := anvildb.NewSelect().
// // Turning *User into an interface &User{}
// Model(&User{}).
// OrderExpr("user_uuid ASC").
// Limit(10).
// Scan(ctx, &users); usererr != nil {
// errors.Join(usererr, err)
// }
// if sshkeyerr := anvildb.NewSelect().
// Model(&SSHKey{}).
// OrderExpr("ssh_key_user_name ASC").
// Scan(ctx, &ssh_keys); sshkeyerr != nil {
// errors.Join(sshkeyerr, err)
// }
// fmt.Printf("##\n## Users\n###\n\n")
// for _, val := range users {
// fmt.Printf("%s\n", val.Username)

@ -0,0 +1 @@
package main

@ -5,7 +5,7 @@ import (
"github.com/uptrace/bun"
)
type User struct {
type UserModel struct {
bun.BaseModel `bun:"table:users"`
UUID uuid.UUID `bun:"user_uuid,pk"`
@ -17,7 +17,7 @@ type User struct {
// users ORDER BY user_uuid;
}
type SSHKey struct {
type SSHKeyModel struct {
bun.BaseModel `bun:"table:ssh_keys"`
UUID uuid.UUID `bun:"ssh_key_uuid,pk"`
@ -27,13 +27,13 @@ type SSHKey struct {
// SELECT ssh_key_uuid,ssh_key_user_name FROM ssh_keys ORDER BY ssh_key_user_name;
}
type Server struct {
type ServerModel struct {
}
type ServerDefinition struct {
type ServerDefinitionModel struct {
}
type Manifest struct {
type ManifestModel struct {
bun.BaseModel `bun:"table:manifests"`
UUID uuid.UUID `bun:"mainifest_uuid,pk"`
@ -44,7 +44,7 @@ type Manifest struct {
// https://pkg.go.dev/encoding/xml#CharData
}
type Job struct {
type JobModel struct {
bun.BaseModel `bun:"table:jobs"`
UUID uuid.UUID `bun:"job_uuid,pk"`
@ -61,7 +61,7 @@ type Job struct {
Status string `bun:"job_status"`
}
type Host struct {
type HostModel struct {
bun.BaseModel `bun:"table:hosts"`
UUID uuid.UUID `bun:"host_uuid,pk"`

@ -0,0 +1,49 @@
package main
import (
"database/sql"
"net/http"
"golang.org/x/crypto/ssh"
)
type Anvil struct {
Strikers []Striker
Hosts []Host
BMCs []BMC
Switches []Switch
PDUs []PDU
UPSes []UPS
}
type Striker struct {
FQDN string
Endpoints struct {
StrikerWebUI *http.Client
Postgresql *sql.DB
SecureShell *ssh.Client
}
}
type Host struct {
FQDN string
Endpoints struct {
SecureShell *ssh.Client
}
}
type BMC struct {
}
type Switch struct {
}
type PDU struct {
}
type UPS struct {
}
//func (d *DB) GetManifests() *SelectQuery {
//
//}
Loading…
Cancel
Save