Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

obs: add library functions - 01 #251

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions obs/error.go
nitishfy marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
Copyright 2023 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
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 obs

import (
"encoding/xml"
"fmt"
)

type Status struct {
XMLName xml.Name `json:"status" xml:"status"`
Code string `json:"code" xml:"code,attr"`
Summary string `json:"summary" xml:"summary"`
}

type APIError struct {
HTTPStatusCode int
OBSStatusCode string
Message string
}

func (e *APIError) Error() string {
return fmt.Sprintf("HTTP status %d: %s (%s)", e.HTTPStatusCode, e.OBSStatusCode, e.Message)
}
111 changes: 109 additions & 2 deletions obs/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ limitations under the License.
package obs

import (
"bytes"
"context"
"encoding/xml"
"fmt"
"net/http"
"net/url"
)

type Project struct {
Expand All @@ -36,6 +41,7 @@ type Project struct {
}

type Client struct {
Client http.Client
Username string
Password string
APIURL string
Expand Down Expand Up @@ -76,8 +82,8 @@ const (

type Repository struct {
Repository string `json:"name" xml:"name,attr"`
Architectures []string `json:"arch" xml:"arch"`
ReleaseTargets []ReleaseTarget `json:"releasetarget,omitempty" xml:"releasetarget,omitempty"`
Architectures []string `json:"architectures" xml:"arch"`
ReleaseTargets []ReleaseTarget `json:"releaseTargets,omitempty" xml:"releasetarget,omitempty"`
Paths []RepositoryPath `json:"path,omitempty" xml:"path,omitempty"`
}

Expand All @@ -91,3 +97,104 @@ type RepositoryPath struct {
Project string `json:"project" xml:"project,attr"`
Repository string `json:"repository" xml:"repository,attr"`
}

func (c *Client) CreateUpdateProject(ctx context.Context, project *Project) error {
xmlData, err := xml.MarshalIndent(project, "", " ")
if err != nil {
return fmt.Errorf("creating obs project: marshalling project meta: %w", err)
}

urlPath, err := url.JoinPath(c.APIURL, "source", project.Name, "_meta")
if err != nil {
return fmt.Errorf("creating obs project: joining url: %w", err)
}

req, err := http.NewRequestWithContext(ctx, http.MethodPut, urlPath, bytes.NewBuffer(xmlData))
if err != nil {
return &APIError{
HTTPStatusCode: 0,
OBSStatusCode: "",
Message: fmt.Sprintf("creating obs project: creating request: %v", err),
}
}

req.SetBasicAuth(c.Username, c.Password)
req.Header.Set("Accept", "application/xml; charset=utf-8")

resp, err := c.Client.Do(req)
if err != nil {
return &APIError{
HTTPStatusCode: 0,
OBSStatusCode: "",
Message: fmt.Sprintf("creating obs project: sending request: %v", err),
}
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
var status Status
if err := xml.NewDecoder(resp.Body).Decode(&status); err != nil {
return &APIError{
HTTPStatusCode: resp.StatusCode,
OBSStatusCode: "",
Message: fmt.Sprintf("creating obs project: decoding error response: %v", err),
}
}

return &APIError{
HTTPStatusCode: resp.StatusCode,
OBSStatusCode: status.Code,
Message: status.Summary,
}
}

return nil
nitishfy marked this conversation as resolved.
Show resolved Hide resolved
}

func (c *Client) DeleteProject(ctx context.Context, project *Project) error {
urlPath, err := url.JoinPath(c.APIURL, "source", project.Name)
if err != nil {
return fmt.Errorf("deleting obs project: joining url: %w", err)
}

req, err := http.NewRequestWithContext(ctx, http.MethodDelete, urlPath, http.NoBody)
if err != nil {
return &APIError{
HTTPStatusCode: 0,
OBSStatusCode: "",
Message: fmt.Sprintf("deleting obs project: creating request: %v", err),
}
}
nitishfy marked this conversation as resolved.
Show resolved Hide resolved

req.SetBasicAuth(c.Username, c.Password)
req.Header.Set("Accept", "application/xml; charset=utf-8")

resp, err := c.Client.Do(req)
if err != nil {
return &APIError{
HTTPStatusCode: 0,
OBSStatusCode: "",
Message: fmt.Sprintf("deleting obs project: sending request: %v", err),
}
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
var status Status
if err := xml.NewDecoder(resp.Body).Decode(&status); err != nil {
return &APIError{
HTTPStatusCode: resp.StatusCode,
OBSStatusCode: "",
Message: fmt.Sprintf("deleting obs project: decoding error response %v", err),
}
}

return &APIError{
nitishfy marked this conversation as resolved.
Show resolved Hide resolved
HTTPStatusCode: resp.StatusCode,
OBSStatusCode: status.Code,
Message: status.Summary,
}
}

return nil
nitishfy marked this conversation as resolved.
Show resolved Hide resolved
nitishfy marked this conversation as resolved.
Show resolved Hide resolved
}