forked from gnuoy/snap-openstack
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
75 changed files
with
5,002 additions
and
1,716 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,14 +37,15 @@ resource "juju_application" "microceph" { | |
units = length(var.machine_ids) # need to manage the number of units | ||
|
||
charm { | ||
name = "microceph" | ||
channel = var.charm_microceph_channel | ||
name = "microceph" | ||
channel = var.charm_microceph_channel | ||
revision = var.charm_microceph_revision | ||
base = "[email protected]" | ||
} | ||
|
||
config = { | ||
config = merge({ | ||
snap-channel = var.microceph_channel | ||
} | ||
}, var.charm_microceph_config) | ||
} | ||
|
||
# juju_offer.microceph_offer will be created | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,16 +37,17 @@ resource "juju_application" "microk8s" { | |
units = length(var.machine_ids) # need to manage the number of units | ||
|
||
charm { | ||
name = "microk8s" | ||
channel = var.charm_microk8s_channel | ||
name = "microk8s" | ||
channel = var.charm_microk8s_channel | ||
revision = var.charm_microk8s_revision | ||
base = "[email protected]" | ||
} | ||
|
||
config = { | ||
config = merge({ | ||
channel = var.microk8s_channel | ||
addons = join(" ", [for key, value in var.addons : "${key}:${value}"]) | ||
disable_cert_reissue = true | ||
kubelet_serialize_image_pulls = false | ||
skip_verify = true | ||
} | ||
}, var.charm_microk8s_config) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,14 +38,15 @@ resource "juju_application" "openstack-hypervisor" { | |
units = length(var.machine_ids) # need to manage the number of units | ||
|
||
charm { | ||
name = "openstack-hypervisor" | ||
channel = var.charm_channel | ||
name = "openstack-hypervisor" | ||
channel = var.charm_channel | ||
revision = var.charm_revision | ||
base = "[email protected]" | ||
} | ||
|
||
config = { | ||
config = merge({ | ||
snap-channel = var.snap_channel | ||
} | ||
}, var.charm_config) | ||
|
||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,9 +33,12 @@ resource "juju_application" "sunbeam-machine" { | |
units = length(var.machine_ids) # need to manage the number of units | ||
|
||
charm { | ||
name = "sunbeam-machine" | ||
channel = var.charm_channel | ||
name = "sunbeam-machine" | ||
channel = var.charm_channel | ||
revision = var.charm_revision | ||
base = "[email protected]" | ||
} | ||
|
||
config = var.charm_config | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,4 +18,6 @@ var Endpoints = []rest.Endpoint{ | |
jujuusersCmd, | ||
jujuuserCmd, | ||
configCmd, | ||
manifestsCmd, | ||
manifestCmd, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"net/url" | ||
|
||
"github.com/canonical/lxd/lxd/response" | ||
"github.com/canonical/lxd/shared/api" | ||
"github.com/canonical/microcluster/rest" | ||
"github.com/canonical/microcluster/state" | ||
"github.com/gorilla/mux" | ||
|
||
"github.com/canonical/snap-openstack/sunbeam-microcluster/api/types" | ||
"github.com/canonical/snap-openstack/sunbeam-microcluster/sunbeam" | ||
) | ||
|
||
// /1.0/manifests endpoint. | ||
var manifestsCmd = rest.Endpoint{ | ||
Path: "manifests", | ||
|
||
Get: rest.EndpointAction{Handler: cmdManifestsGetAll, ProxyTarget: true, AllowUntrusted: true}, | ||
Post: rest.EndpointAction{Handler: cmdManifestsPost, ProxyTarget: true, AllowUntrusted: true}, | ||
} | ||
|
||
// /1.0/manifests/<manifestid> endpoint. | ||
// /1.0/manifests/latest will give the latest inserted manifest record | ||
var manifestCmd = rest.Endpoint{ | ||
Path: "manifests/{manifestid}", | ||
|
||
Get: rest.EndpointAction{Handler: cmdManifestGet, ProxyTarget: true, AllowUntrusted: true}, | ||
Delete: rest.EndpointAction{Handler: cmdManifestDelete, ProxyTarget: true, AllowUntrusted: true}, | ||
} | ||
|
||
func cmdManifestsGetAll(s *state.State, _ *http.Request) response.Response { | ||
|
||
manifests, err := sunbeam.ListManifests(s) | ||
if err != nil { | ||
return response.InternalError(err) | ||
} | ||
|
||
return response.SyncResponse(true, manifests) | ||
} | ||
|
||
func cmdManifestGet(s *state.State, r *http.Request) response.Response { | ||
var manifestid string | ||
manifestid, err := url.PathUnescape(mux.Vars(r)["manifestid"]) | ||
if err != nil { | ||
return response.InternalError(err) | ||
} | ||
manifest, err := sunbeam.GetManifest(s, manifestid) | ||
if err != nil { | ||
if err, ok := err.(api.StatusError); ok { | ||
if err.Status() == http.StatusNotFound { | ||
return response.NotFound(err) | ||
} | ||
} | ||
return response.InternalError(err) | ||
} | ||
|
||
return response.SyncResponse(true, manifest) | ||
} | ||
|
||
func cmdManifestsPost(s *state.State, r *http.Request) response.Response { | ||
var req types.Manifest | ||
|
||
err := json.NewDecoder(r.Body).Decode(&req) | ||
if err != nil { | ||
return response.InternalError(err) | ||
} | ||
|
||
err = sunbeam.AddManifest(s, req.ManifestID, req.Data) | ||
if err != nil { | ||
return response.InternalError(err) | ||
} | ||
|
||
return response.EmptySyncResponse | ||
} | ||
|
||
func cmdManifestDelete(s *state.State, r *http.Request) response.Response { | ||
manifestid, err := url.PathUnescape(mux.Vars(r)["manifestid"]) | ||
if err != nil { | ||
return response.SmartError(err) | ||
} | ||
err = sunbeam.DeleteManifest(s, manifestid) | ||
if err != nil { | ||
return response.InternalError(err) | ||
} | ||
|
||
return response.EmptySyncResponse | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Package types provides shared types and structs. | ||
package types | ||
|
||
// Manifests holds list of manifest type | ||
type Manifests []Manifest | ||
|
||
// Manifest structure to hold manifest applytime and manifest data | ||
type Manifest struct { | ||
ManifestID string `json:"manifestid" yaml:"manifestid"` | ||
AppliedDate string `json:"applieddate" yaml:"applieddate"` | ||
Data string `json:"data" yaml:"data"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package database | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/canonical/lxd/shared/api" | ||
"github.com/canonical/microcluster/cluster" | ||
) | ||
|
||
//go:generate -command mapper lxd-generate db mapper -t manifest.mapper.go | ||
//go:generate mapper reset | ||
// | ||
//go:generate mapper stmt -d github.com/canonical/microcluster/cluster -e ManifestItem objects table=manifest | ||
//go:generate mapper stmt -d github.com/canonical/microcluster/cluster -e ManifestItem objects-by-ManifestID table=manifest | ||
//go:generate mapper stmt -d github.com/canonical/microcluster/cluster -e ManifestItem id table=manifest | ||
//go:generate mapper stmt -d github.com/canonical/microcluster/cluster -e ManifestItem delete-by-ManifestID table=manifest | ||
|
||
// | ||
//go:generate mapper method -i -d github.com/canonical/microcluster/cluster -e ManifestItem GetMany table=manifest | ||
//go:generate mapper method -i -d github.com/canonical/microcluster/cluster -e ManifestItem GetOne table=manifest | ||
//go:generate mapper method -i -d github.com/canonical/microcluster/cluster -e ManifestItem ID table=manifest | ||
//go:generate mapper method -i -d github.com/canonical/microcluster/cluster -e ManifestItem Exists table=manifest | ||
//go:generate mapper method -i -d github.com/canonical/microcluster/cluster -e ManifestItem DeleteOne-by-ManifestID table=manifest | ||
|
||
// ManifestItem is used to save the Sunbeam manifests provided by user. | ||
// AppliedDate is saved as Timestamp in database but retreived as string | ||
// Probable Bug: https://github.com/mattn/go-sqlite3/issues/951 | ||
type ManifestItem struct { | ||
ID int | ||
ManifestID string `db:"primary=yes"` | ||
AppliedDate string | ||
Data string | ||
} | ||
|
||
// ManifestItemFilter is a required struct for use with lxd-generate. It is used for filtering fields on database fetches. | ||
type ManifestItemFilter struct { | ||
ManifestID *string | ||
} | ||
|
||
var manifestItemCreate = cluster.RegisterStmt(` | ||
INSERT INTO manifest (manifest_id, data) | ||
VALUES (?, ?) | ||
`) | ||
|
||
var latestManifestItemObject = cluster.RegisterStmt(` | ||
SELECT manifest.id, manifest.manifest_id, manifest.applied_date, manifest.data | ||
FROM manifest | ||
WHERE manifest.applied_date = (SELECT MAX(applied_date) FROM manifest) | ||
`) | ||
|
||
// CreateManifestItem adds a new ManifestItem to the database. | ||
// generator: ManifestItem Create | ||
func CreateManifestItem(ctx context.Context, tx *sql.Tx, object ManifestItem) (int64, error) { | ||
// Check if a ManifestItem with the same key exists. | ||
exists, err := ManifestItemExists(ctx, tx, object.ManifestID) | ||
if err != nil { | ||
return -1, fmt.Errorf("Failed to check for duplicates: %w", err) | ||
} | ||
|
||
if exists { | ||
return -1, api.StatusErrorf(http.StatusConflict, "This \"manifest\" entry already exists") | ||
} | ||
|
||
args := make([]any, 2) | ||
|
||
// Populate the statement arguments. | ||
args[0] = object.ManifestID | ||
args[1] = object.Data | ||
|
||
// Prepared statement to use. | ||
stmt, err := cluster.Stmt(tx, manifestItemCreate) | ||
if err != nil { | ||
return -1, fmt.Errorf("Failed to get \"manifestItemCreate\" prepared statement: %w", err) | ||
} | ||
|
||
// Execute the statement. | ||
result, err := stmt.Exec(args...) | ||
if err != nil { | ||
return -1, fmt.Errorf("Failed to create \"manifest\" entry: %w", err) | ||
} | ||
|
||
id, err := result.LastInsertId() | ||
if err != nil { | ||
return -1, fmt.Errorf("Failed to fetch \"manifest\" entry ID: %w", err) | ||
} | ||
|
||
return id, nil | ||
} | ||
|
||
// GetLatestManifestItem returns the latest inserted record in manifest table. | ||
func GetLatestManifestItem(ctx context.Context, tx *sql.Tx) (*ManifestItem, error) { | ||
var err error | ||
|
||
// Pick the prepared statement and arguments to use based on active criteria. | ||
var sqlStmt *sql.Stmt | ||
|
||
sqlStmt, err = cluster.Stmt(tx, latestManifestItemObject) | ||
if err != nil { | ||
return nil, fmt.Errorf("Failed to get \"manifestItemObjects\" prepared statement: %w", err) | ||
} | ||
|
||
// Result slice. | ||
// objects := make([]ManifestItem, 0) | ||
objects, err := getManifestItems(ctx, sqlStmt) | ||
if err != nil { | ||
return nil, fmt.Errorf("Failed to fetch from \"manifest\" table: %w", err) | ||
} | ||
|
||
objectsLen := len(objects) | ||
switch objectsLen { | ||
case 0: | ||
return nil, api.StatusErrorf(http.StatusNotFound, "ManifestItem not found") | ||
default: | ||
return &objects[objectsLen-1], nil | ||
} | ||
} |
Oops, something went wrong.