summaryrefslogtreecommitdiff
path: root/pkg/bindings/generate/generate.go
blob: dde1cc29c3265f3db8a81208030aeee620e6654e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package generate

import (
	"context"
	"net/http"
	"net/url"
	"strconv"

	"github.com/containers/podman/v2/pkg/bindings"
	"github.com/containers/podman/v2/pkg/domain/entities"
)

func Systemd(ctx context.Context, nameOrID string, options entities.GenerateSystemdOptions) (*entities.GenerateSystemdReport, error) {
	conn, err := bindings.GetClient(ctx)
	if err != nil {
		return nil, err
	}
	params := url.Values{}

	params.Set("useName", strconv.FormatBool(options.Name))
	params.Set("new", strconv.FormatBool(options.New))
	if options.RestartPolicy != "" {
		params.Set("restartPolicy", options.RestartPolicy)
	}
	if options.StopTimeout != nil {
		params.Set("stopTimeout", strconv.FormatUint(uint64(*options.StopTimeout), 10))
	}
	params.Set("containerPrefix", options.ContainerPrefix)
	params.Set("podPrefix", options.PodPrefix)
	params.Set("separator", options.Separator)

	response, err := conn.DoRequest(nil, http.MethodGet, "/generate/%s/systemd", params, nil, nameOrID)
	if err != nil {
		return nil, err
	}
	report := &entities.GenerateSystemdReport{}
	return report, response.Process(&report.Units)
}

func Kube(ctx context.Context, nameOrID string, options entities.GenerateKubeOptions) (*entities.GenerateKubeReport, error) {
	conn, err := bindings.GetClient(ctx)
	if err != nil {
		return nil, err
	}
	params := url.Values{}
	params.Set("service", strconv.FormatBool(options.Service))

	response, err := conn.DoRequest(nil, http.MethodGet, "/generate/%s/kube", params, nil, nameOrID)
	if err != nil {
		return nil, err
	}

	if response.StatusCode == http.StatusOK {
		return &entities.GenerateKubeReport{Reader: response.Body}, nil
	}

	// Unpack the error.
	return nil, response.Process(nil)
}