Skip to content

Commit

Permalink
add initial library functions: DELETE
Browse files Browse the repository at this point in the history
  • Loading branch information
nitishfy committed Sep 26, 2023
1 parent f318469 commit 07ec6b9
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 2 deletions.
38 changes: 38 additions & 0 deletions obs/error.go
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(project *Project, ctx context.Context) 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
}

func (c *Client) DeleteProject(project *Project, ctx context.Context) 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),
}
}

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{
HTTPStatusCode: resp.StatusCode,
OBSStatusCode: status.Code,
Message: status.Summary,
}
}

return nil
}

0 comments on commit 07ec6b9

Please sign in to comment.