Skip to content

Commit

Permalink
Merge pull request #27 from hallyn/2023-09-26/api
Browse files Browse the repository at this point in the history
add machine client api pkg
  • Loading branch information
raharper authored Oct 10, 2023
2 parents 792a924 + 1b247c8 commit 1b7bc6e
Show file tree
Hide file tree
Showing 19 changed files with 123 additions and 37 deletions.
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ all: $(BINS)
clean:
rm -f -v $(BINS)

bin/machine: cmd/machine/*.go cmd/machine/cmd/*.go pkg/*/*.go
go build -o $@ cmd/machine/*.go
bin/machine: cmd/machine/cmd/*.go pkg/*/*.go
go build -o $@ cmd/machine/cmd/*.go

bin/machined: cmd/machined/*.go cmd/machined/cmd/*.go pkg/*/*.go
go build -o $@ cmd/machined/*.go
bin/machined: cmd/machined/cmd/*.go pkg/*/*.go
go build -o $@ cmd/machined/cmd/*.go
4 changes: 2 additions & 2 deletions cmd/machine/cmd/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd
package main

import (
"encoding/json"
"fmt"
"machine/pkg/api"
"os"
"os/exec"

log "github.com/sirupsen/logrus"
"github.com/project-machine/machine/pkg/api"
"github.com/spf13/cobra"
)

Expand Down
4 changes: 2 additions & 2 deletions cmd/machine/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd
package main

import (
"fmt"
"machine/pkg/api"

"github.com/spf13/cobra"
"github.com/project-machine/machine/pkg/api"
)

// deleteCmd represents the list command
Expand Down
4 changes: 2 additions & 2 deletions cmd/machine/cmd/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd
package main

import (
"fmt"
"machine/pkg/api"
"os"

"github.com/lxc/lxd/shared"
"github.com/lxc/lxd/shared/termios"
"github.com/project-machine/machine/pkg/api"
"github.com/spf13/cobra"
"golang.org/x/sys/unix"
"gopkg.in/yaml.v2"
Expand Down
6 changes: 3 additions & 3 deletions cmd/machine/cmd/gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd
package main

import (
"machine/pkg/api"

"github.com/spf13/cobra"

"github.com/project-machine/machine/pkg/api"
)

// guiCmd represents the gui command
Expand Down
2 changes: 1 addition & 1 deletion cmd/machine/cmd/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd
package main

import (
"fmt"
Expand Down
4 changes: 2 additions & 2 deletions cmd/machine/cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd
package main

import (
"fmt"
"io/ioutil"
"machine/pkg/api"
"os"
"path/filepath"
"sort"
Expand All @@ -26,6 +25,7 @@ import (
"github.com/lxc/lxd/shared"
"github.com/lxc/lxd/shared/termios"
homedir "github.com/mitchellh/go-homedir"
"github.com/project-machine/machine/pkg/api"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"golang.org/x/sys/unix"
Expand Down
2 changes: 1 addition & 1 deletion cmd/machine/cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd
package main

import (
"fmt"
Expand Down
4 changes: 1 addition & 3 deletions cmd/machine/main.go → cmd/machine/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ limitations under the License.
*/
package main

import "machine/cmd/machine/cmd"

func main() {
cmd.Execute()
Execute()
}
4 changes: 2 additions & 2 deletions cmd/machine/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd
package main

import (
"context"
"encoding/json"
"fmt"
"machine/pkg/api"
"math/rand"
"net"
"net/http"
Expand All @@ -27,6 +26,7 @@ import (
"time"

"github.com/go-resty/resty/v2"
"github.com/project-machine/machine/pkg/api"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand Down
2 changes: 1 addition & 1 deletion cmd/machine/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd
package main

import (
"fmt"
Expand Down
4 changes: 2 additions & 2 deletions cmd/machine/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd
package main

import (
"fmt"
"machine/pkg/api"

"github.com/project-machine/machine/pkg/api"
"github.com/spf13/cobra"
)

Expand Down
4 changes: 2 additions & 2 deletions cmd/machine/cmd/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd
package main

import (
"fmt"
"machine/pkg/api"

"github.com/project-machine/machine/pkg/api"
"github.com/spf13/cobra"
)

Expand Down
4 changes: 2 additions & 2 deletions cmd/machined/cmd/install.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package cmd
package main

import (
"fmt"
"machine/pkg/api"
"os"
"os/exec"
"path/filepath"

"github.com/project-machine/machine/pkg/api"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
Expand Down
4 changes: 1 addition & 3 deletions cmd/machined/main.go → cmd/machined/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ limitations under the License.
*/
package main

import "machine/cmd/machined/cmd"

func main() {
cmd.Execute()
Execute()
}
4 changes: 2 additions & 2 deletions cmd/machined/cmd/remove.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package cmd
package main

import (
"fmt"
"machine/pkg/api"
"os"
"os/exec"
"path/filepath"

"github.com/project-machine/machine/pkg/api"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
Expand Down
4 changes: 2 additions & 2 deletions cmd/machined/cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package cmd
package main

import (
"context"
"fmt"
"io/ioutil"
"machine/pkg/api"
"math/rand"
"net/http"
"os"
Expand All @@ -14,6 +13,7 @@ import (
"text/template"
"time"

"github.com/project-machine/machine/pkg/api"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module machine
module github.com/project-machine/machine

go 1.18

Expand Down
90 changes: 90 additions & 0 deletions pkg/client/machine.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package client

import (
"context"
"encoding/json"
"fmt"
"math/rand"
"path/filepath"
"time"
"net"
"net/http"

"github.com/go-resty/resty/v2"

"github.com/project-machine/machine/pkg/api"
)

var rootclient *resty.Client

func init() {
rand.Seed(time.Now().UTC().UnixNano())

// configure the http client to point to the unix socket
apiSocket := api.APISocketPath()
if len(apiSocket) == 0 {
panic("Failed to get API socket path")
}

unixDial := func(_ context.Context, network, addr string) (net.Conn, error) {
raddr, err := net.ResolveUnixAddr("unix", apiSocket)
if err != nil {
return nil, err
}

return net.DialUnix("unix", nil, raddr)
}

transport := http.Transport{
DialContext: unixDial,
DisableKeepAlives: true,
ExpectContinueTimeout: time.Second * 30,
ResponseHeaderTimeout: time.Second * 3600,
TLSHandshakeTimeout: time.Second * 5,
}

rootclient = resty.New()
rootclient.SetTransport(&transport).SetScheme("http").SetBaseURL(apiSocket)
}

func GetMachines() ([]api.Machine, error) {
machines := []api.Machine{}
listURL := api.GetAPIURL("machines")
if len(listURL) == 0 {
return machines, fmt.Errorf("Failed to get API URL for 'machines' endpoint")
}
resp, _ := rootclient.R().EnableTrace().Get(listURL)
err := json.Unmarshal(resp.Body(), &machines)
if err != nil {
return machines, fmt.Errorf("Failed to unmarshal GET on /machines")
}
return machines, nil
}

func GetMachine(machineName string) (api.Machine, int, error) {
machine := api.Machine{}
getURL := api.GetAPIURL(filepath.Join("machines", machineName))
if len(getURL) == 0 {
return machine, http.StatusBadRequest, fmt.Errorf("Failed to get API URL for 'machines/%s' endpoint", machineName)
}
resp, _ := rootclient.R().EnableTrace().Get(getURL)
err := json.Unmarshal(resp.Body(), &machine)
if err != nil {
return machine, resp.StatusCode(), fmt.Errorf("%d: Failed to unmarshal GET on /machines/%s", resp.StatusCode(), machineName)
}
return machine, resp.StatusCode(), nil
}

func PutMachine(newMachine api.Machine) error {
endpoint := fmt.Sprintf("machines/%s", newMachine.Name)
putURL := api.GetAPIURL(endpoint)
if len(putURL) == 0 {
return fmt.Errorf("Failed to get API PUT URL for 'machines' endpoint")
}
resp, err := rootclient.R().EnableTrace().SetBody(newMachine).Put(putURL)
if err != nil {
return fmt.Errorf("Failed PUT to machine '%s' endpoint: %s", newMachine.Name, err)
}
fmt.Printf("%s %s\n", resp, resp.Status())
return nil
}

0 comments on commit 1b7bc6e

Please sign in to comment.