client: Add projects/source API support

This commit is contained in:
Brian C. Lane 2020-03-18 08:53:11 -07:00 committed by Tom Gundersen
parent 971bafbc09
commit 3005486cb4
2 changed files with 115 additions and 0 deletions

69
internal/client/source.go Normal file
View file

@ -0,0 +1,69 @@
// Package client - source contains functions for the source API
// Copyright (C) 2020 by Red Hat, Inc.
package client
import (
"encoding/json"
// "fmt"
"net/http"
// "strings"
"github.com/osbuild/osbuild-composer/internal/weldr"
)
// ListSourcesV0 returns a list of source names
func ListSourcesV0(socket *http.Client) ([]string, *APIResponse, error) {
body, resp, err := GetRaw(socket, "GET", "/api/v0/projects/source/list")
if resp != nil || err != nil {
return nil, resp, err
}
var list weldr.SourceListV0
err = json.Unmarshal(body, &list)
if err != nil {
return nil, nil, err
}
return list.Sources, nil, nil
}
// GetSourceInfoV0 returns detailed information on the named sources
func GetSourceInfoV0(socket *http.Client, sourceNames string) (map[string]weldr.SourceConfigV0, *APIResponse, error) {
body, resp, err := GetRaw(socket, "GET", "/api/v0/projects/source/info/"+sourceNames)
if resp != nil || err != nil {
return nil, resp, err
}
var info weldr.SourceInfoV0
err = json.Unmarshal(body, &info)
if err != nil {
return nil, nil, err
}
return info.Sources, nil, nil
}
// PostJSONSourceV0 sends a JSON source string to the API
// and returns an APIResponse
func PostJSONSourceV0(socket *http.Client, source string) (*APIResponse, error) {
body, resp, err := PostJSON(socket, "/api/v0/projects/source/new", source)
if resp != nil || err != nil {
return resp, err
}
return NewAPIResponse(body)
}
// PostTOMLSourceV0 sends a TOML source string to the API
// and returns an APIResponse
func PostTOMLSourceV0(socket *http.Client, source string) (*APIResponse, error) {
body, resp, err := PostTOML(socket, "/api/v0/projects/source/new", source)
if resp != nil || err != nil {
return resp, err
}
return NewAPIResponse(body)
}
// DeleteSourceV0 deletes the named source and returns an APIResponse
func DeleteSourceV0(socket *http.Client, sourceName string) (*APIResponse, error) {
body, resp, err := DeleteRaw(socket, "/api/v0/projects/source/delete/"+sourceName)
if resp != nil || err != nil {
return resp, err
}
return NewAPIResponse(body)
}

View file

@ -5,6 +5,7 @@ package weldr
import (
"github.com/osbuild/osbuild-composer/internal/blueprint"
"github.com/osbuild/osbuild-composer/internal/rpmmd"
"github.com/osbuild/osbuild-composer/internal/store"
)
// StatusV0 is the response to /api/status from a v0+ server
@ -75,3 +76,48 @@ type BlueprintsFreezeV0 struct {
type blueprintFrozen struct {
Blueprint blueprint.Blueprint `json:"blueprint"`
}
// SourceListV0 is the response to /source/list request
type SourceListV0 struct {
Sources []string `json:"sources"`
}
// SourceInfoV0 is the response to a /source/info request
type SourceInfoV0 struct {
Sources map[string]SourceConfigV0 `json:"sources"`
Errors []ResponseError `json:"errors"`
}
// SourceConfig returns a SourceConfig struct populated with the supported variables
func (s *SourceInfoV0) SourceConfig(sourceName string) (ssc store.SourceConfig, ok bool) {
si, ok := s.Sources[sourceName]
if !ok {
return ssc, false
}
return si.SourceConfig(), true
}
// SourceConfigV0 holds the source repository information
type SourceConfigV0 struct {
Name string `json:"name"`
Type string `json:"type"`
URL string `json:"url"`
CheckGPG bool `json:"check_gpg"`
CheckSSL bool `json:"check_ssl"`
System bool `json:"system"`
Proxy string `json:"proxy"`
GPGUrls []string `json:"gpgkey_urls"`
}
// SourceConfig returns a SourceConfig struct populated with the supported variables
// The store does not support proxy and gpgkey_urls
func (s *SourceConfigV0) SourceConfig() (ssc store.SourceConfig) {
ssc.Name = s.Name
ssc.Type = s.Type
ssc.URL = s.URL
ssc.CheckGPG = s.CheckGPG
ssc.CheckSSL = s.CheckSSL
return ssc
}