Compare commits

...

4 Commits

Author SHA1 Message Date
Mike Holloway
09c1bd44c9 Scope adustments 2024-08-26 02:14:20 -04:00
Mike Holloway
2f56639f5b Trying struct literals and other methods of value setting. 2024-08-25 22:46:44 -04:00
Mike Holloway
84155e8590 Renaming vars 2024-08-25 17:11:39 -04:00
Mike Holloway
2f095a8928 new file: anvilssh.go
Testing Anvil ssh struct
modified:   go.mod
modified:   go.sum
modified:   main.go
modified:   models.go
	Challenge: storing AuthorizedKey record as ssh datatype
2024-08-25 17:08:17 -04:00
9 changed files with 171 additions and 107 deletions

View File

@ -1,24 +0,0 @@
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)

1
db.go Normal file
View File

@ -0,0 +1 @@
package main

14
functions.go Normal file
View File

@ -0,0 +1,14 @@
package main
import (
"database/sql"
"github.com/uptrace/bun"
"github.com/uptrace/bun/dialect/pgdialect"
"github.com/uptrace/bun/driver/pgdriver"
)
func OpenDB(dsn string) *sql.DB {
sqldb := sql.OpenDB(pgdriver.NewConnector(pgdriver.WithDSN(dsn)))
return bun.NewDB(sqldb, pgdialect.New())
}

4
go.mod
View File

@ -3,10 +3,13 @@ module anvil-client
go 1.21.10
require (
github.com/google/uuid v1.6.0
github.com/uptrace/bun v1.2.1
github.com/uptrace/bun/dialect/pgdialect v1.2.1
github.com/uptrace/bun/driver/pgdriver v1.2.1
github.com/uptrace/bun/extra/bundebug v1.2.1
golang.org/x/crypto v0.21.0
golang.org/x/term v0.18.0
)
require (
@ -17,7 +20,6 @@ require (
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc // indirect
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/sys v0.18.0 // indirect
mellium.im/sasl v0.3.1 // indirect
)

4
go.sum
View File

@ -2,6 +2,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
@ -33,6 +35,8 @@ golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.18.0 h1:FcHjZXDMxI8mM3nwhX9HlKop4C0YQvCVCdwYl2wOtE8=
golang.org/x/term v0.18.0/go.mod h1:ILwASektA3OnRv7amZ1xhE/KTR+u50pbXfZ03+6Nx58=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
mellium.im/sasl v0.3.1 h1:wE0LW6g7U83vhvxjC1IY8DnXM+EU095yeo8XClvCdfo=

93
main.go
View File

@ -2,71 +2,42 @@ package main
import (
"context"
"errors"
"fmt"
)
var feeds []Feed
var categories []Category
var entries []Entry
var users []User
// 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
//var jobs []Job
//var hosts []Host
func main() {
var err error
ctx := context.Background()
dsn := "postgres://apache:@localhost:5432/miniflux?sslmode=disable"
// dsn := "unix://user:pass@dbname/var/run/postgresql/.s.PGSQL.5432"
var (
ctx = context.Background()
dsn = "postgres://postgres:Initial1@localhost:15432/anvil?sslmode=disable"
// dsn := "unix://user:pass@dbname/var/run/postgresql/.s.PGSQL.5432"
)
// Calling PostgresClient as an instance method as per:
// https://stackoverflow.com/questions/38666404/not-enough-arguments-in-call-to-method-expression
anvildb := OpenDB(dsn)
anvildb := ConnectAnvilDB(dsn)
// Turn these into funcs with error handling, logging, return vals etc.
feederr := anvildb.NewSelect().
Model(&Feed{}).
OrderExpr("title ASC").
Limit(10).
Scan(ctx, &feeds)
if feederr != nil {
errors.Join(feederr, err)
}
categoryerr := anvildb.NewSelect().
Model(&Category{}).
OrderExpr("title ASC").
Scan(ctx, &categories)
if categoryerr != nil {
errors.Join(categoryerr, err)
}
entryerr := anvildb.NewSelect().
Model(&Entry{}).
OrderExpr("published_at ASC").
Limit(10).
Scan(ctx, &entries)
if entryerr != nil {
errors.Join(entryerr, err)
}
usererr := anvildb.NewSelect().
Model(&User{}).
OrderExpr("id ASC").
Scan(ctx, &users)
if usererr != nil {
errors.Join(usererr, err)
}
for _, val := range entries {
fmt.Printf("val = %s\n", val)
}
for _, val := range categories {
fmt.Printf("val = %s\n", val)
}
for _, val := range feeds {
fmt.Printf("val = %s\n", val)
}
for _, val := range users {
fmt.Printf("val = %s\n", val)
}
// fmt.Printf("%+v\n", entries)
// fmt.Printf("%+v\n", categories)
// fmt.Printf("%+v\n", feeds)
// fmt.Printf("%+v\n", users)
// if usererr := anvildb.NewSelect().
// // Turning *User into an interface &User{}
// Model(&UserModel{}).
// OrderExpr("user_uuid ASC").
// Limit(10).
// Scan(ctx, &users); usererr != nil {
// errors.Join(usererr, err)
// }
// fmt.Printf("##\n## Users\n###\n\n")
// for _, val := range users {
// fmt.Printf("%s\n", val.Username)
// }
}

View File

@ -1,37 +1,73 @@
package main
import (
"github.com/google/uuid"
"github.com/uptrace/bun"
)
type User struct {
type UserModel struct {
bun.BaseModel `bun:"table:users"`
ID int64 `bun:"id,pk,autoincrement"`
Username string `bun:"username"`
UUID uuid.UUID `bun:"user_uuid,pk"`
Username string `bun:"user_name"`
IsExperienced bool `bun:"user_is_experienced"`
IsTrusted bool `bun:"user_is_trusted"`
// https://pkg.go.dev/github.com/google/uuid#UUID
// SELECT user_uuid,user_name,user_is_experienced,user_is_trusted FROM \
// users ORDER BY user_uuid;
}
type Feed struct {
bun.BaseModel `bun:"table:feeds"`
type SSHKeyModel struct {
bun.BaseModel `bun:"table:ssh_keys"`
ID int64 `bun:"id,pk,autoincrement"`
CategoryID int64 `bun:"category_id"`
Title string `bun:"title"`
// SELECT id,category_id,title FROM feeds ORDER BY id;
UUID uuid.UUID `bun:"ssh_key_uuid,pk"`
Username string `bun:"ssh_key_user_name"`
PublicKey []byte `bun:"ssh_key_public_key"`
// https://pkg.go.dev/golang.org/x/crypto/ssh#PublicKey
// SELECT ssh_key_uuid,ssh_key_user_name FROM ssh_keys ORDER BY ssh_key_user_name;
}
type Entry struct {
bun.BaseModel `bun:"table:entries"`
ID int64 `bun:"id,pk,autoincrement"`
Title string `bun:"title"`
Author string `bun:"author"`
// SELECT id,title,author FROM entries ORDER BY published_at LIMIT 10;
type ServerModel struct {
}
type Category struct {
bun.BaseModel `bun:"table:categories"`
ID int64 `bun:"id,pk,autoincrement"`
Title string `bun:"title"`
//SELECT id,title from categories;
type ServerDefinitionModel struct {
}
type ManifestModel struct {
bun.BaseModel `bun:"table:manifests"`
UUID uuid.UUID `bun:"mainifest_uuid,pk"`
Name string `bun:"manifest_name"`
LastRan string `bun:"manifest_last_ran"`
XML string `bun:"manifest_xml"`
Note string `bun:"manifest_note"`
// https://pkg.go.dev/encoding/xml#CharData
}
type JobModel struct {
bun.BaseModel `bun:"table:jobs"`
UUID uuid.UUID `bun:"job_uuid,pk"`
HostUUID uuid.UUID `bun:"host_uuid"`
Command string `bun:"job_command"`
Data string `bun:"job_data"`
PickedUpBy string `bun:"job_picked_up_by"`
PickedUpAt string `bun:"job_picked_up_at"`
Updated string `bun:"job_updated"`
Name string `bun:"job_name"`
Progress int64 `bun:"job_progress"`
Title string `bun:"job_title"`
Description string `bun:"job_description"`
Status string `bun:"job_status"`
}
type HostModel struct {
bun.BaseModel `bun:"table:hosts"`
UUID uuid.UUID `bun:"host_uuid,pk"`
HostName string `bun:"host_name"`
HostType string `bun:"host_type"`
HostKey string `bun:"host_key"`
HostIPMI string `bun:"host_ipmi"`
HostStatus string `bun:"host_status"`
}

11
ssh.go Normal file
View File

@ -0,0 +1,11 @@
package main
import (
"golang.org/x/crypto/ssh"
"golang.org/x/term"
)
type AnvilSSHConnection struct {
thing ssh.Client
otherthing term.Terminal
}

49
types.go Normal file
View File

@ -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 WebClient struct {
*http.Client
}
type SSHClient struct {
*ssh.Client
}
type PostgresClient struct {
*sql.DB
}
type Striker struct {
FQDN string
}
type Host struct {
FQDN string
}
type BMC struct {
}
type Switch struct {
}
type PDU struct {
}
type UPS struct {
}