github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/servicestage/v2/components/requests.go (about) 1 package components 2 3 import ( 4 "github.com/chnsz/golangsdk" 5 "github.com/chnsz/golangsdk/pagination" 6 ) 7 8 var requestOpts golangsdk.RequestOpts = golangsdk.RequestOpts{ 9 MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"}, 10 } 11 12 // CreateOpts is the structure required by the Create method to create a new component. 13 type CreateOpts struct { 14 // Application component name. 15 // The value can contain 2 to 64 characters, including letters, digits, hyphens (-), and underscores (_). 16 // It must start with a letter and end with a letter or digit. 17 Name string `json:"name" required:"true"` 18 // Runtime. 19 // The value can be obtained from type_name returned by the API in Obtaining All Supported Runtimes of Application 20 // Components. 21 Runtime string `json:"runtime" required:"true"` 22 // Application component type. Example: Webapp, MicroService, or Common. 23 Type string `json:"category" required:"true"` 24 // Application component sub-type. 25 // Webapp sub-types include Web. 26 // MicroService sub-types include Java Chassis, Go Chassis, Mesher, Spring Cloud, and Dubbo. 27 // Common sub-type can be empty. 28 Framwork string `json:"sub_category,omitempty"` 29 // Description. 30 // The value can contain up to 128 characters. 31 Description string `json:"description,omitempty"` 32 // Source of the code or software package. 33 Source *Source `json:"source,omitempty"` 34 // Component builder. 35 Builder *Builder `json:"build,omitempty"` 36 } 37 38 // Source is an object to specified the source information of Open-Scoure codes or package storage. 39 type Source struct { 40 // Type. Option: source code or artifact software package. 41 Kind string `json:"kind" required:"true"` 42 // The details about the Repository source code and the Artifact software package. 43 Spec Spec `json:"spec" required:"true"` 44 } 45 46 // Spec is an object to specified the configuration of repository or storage. 47 type Spec struct { 48 // The parameters of code are as follows: 49 // Code repository. Value: GitHub, GitLab, Gitee, or Bitbucket. 50 RepoType string `json:"repo_type,omitempty"` 51 // Code repository URL. Example: https://github.com/example/demo.git. 52 RepoUrl string `json:"repo_url,omitempty"` 53 // Authorization name, which can be obtained from the authorization list. 54 RepoAuth string `json:"repo_auth,omitempty"` 55 // The code's organization. Value: GitHub, GitLab, Gitee, or Bitbucket. 56 RepoNamespace string `json:"repo_namespace,omitempty"` 57 // Code branch or tag. Default value: master. 58 RepoRef string `json:"repo_ref,omitempty"` 59 60 // The parameters of artifact are as follows: 61 // Storage mode. Value: swr or obs. 62 Storage string `json:"storage,omitempty"` 63 // Type. Value: package. 64 Type string `json:"type,omitempty"` 65 // Address of the software package or source code. 66 Url string `json:"url,omitempty"` 67 // Authentication mode. Value: iam or none. Default value: iam. 68 Auth string `json:"auth,omitempty"` 69 // Other attributes of the software package. You need to add these attributes only when you set storage to obs. 70 Properties Properties `json:"properties,omitempty"` 71 } 72 73 // Properties is an object to specified the other configuration of the software package for OBS bucket. 74 type Properties struct { 75 // Object Storage Service (OBS) endpoint address. Example: https://obs.region_id.external_domain_name.com. 76 Endpoint string `json:"endpoint,omitempty"` 77 // Name of the OBS bucket where the software package is stored. 78 Bucket string `json:"bucket,omitempty"` 79 // Object in the OBS bucket, which is usually the name of the software package. 80 // If there is a folder, the path of the folder must be added. Example: test.jar or demo/test.jar. 81 Key string `json:"key,omitempty"` 82 } 83 84 // Builder is the component builder, the configuration details refer to 'Parameter'. 85 type Builder struct { 86 // This parameter is provided only when no ID is available during build creation. 87 Parameter Parameter `json:"parameters" required:"true"` 88 } 89 90 // Parameter is an object to specified the building configuration of codes or package. 91 type Parameter struct { 92 // Compilation command. By default: 93 // When build.sh exists in the root directory, the command is ./build.sh. 94 // When build.sh does not exist in the root directory, the command varies depending on the operating system (OS). Example: 95 // Java and Tomcat: mvn clean package 96 // Nodejs: npm build 97 BuildCmd string `json:"build_cmd,omitempty"` 98 // Address of the Docker file. By default, the Docker file is in the root directory (./). 99 DockerfilePath string `json:"dockerfile_path,omitempty"` 100 // Build archive organization. Default value: cas_{project_id}. 101 ArtifactNamespace string `json:"artifact_namespace,omitempty"` 102 // The ID of the cluster to be built. 103 ClusterId string `json:"cluster_id,omitempty"` 104 // The name of the cluster to be built. 105 ClusterName string `json:"clsuter_name,omitempty"` 106 // The type of the cluster to be built. 107 ClusterType string `json:"cluster_type,omitempty"` 108 // key indicates the key of the tag, and value indicates the value of the tag. 109 UsePublicCluster bool `json:"use_public_cluster,omitempty"` 110 // key indicates the key of the tag, and value indicates the value of the tag. 111 NodeLabelSelector map[string]interface{} `json:"node_label_selector,omitempty"` 112 } 113 114 // Create is a method to create a component in the specified appliation using given parameters. 115 func Create(c *golangsdk.ServiceClient, appId string, opts CreateOpts) (*Component, error) { 116 b, err := golangsdk.BuildRequestBody(opts, "") 117 if err != nil { 118 return nil, err 119 } 120 121 var r Component 122 _, err = c.Post(rootURL(c, appId), b, &r, nil) 123 return &r, err 124 } 125 126 // Get is a method to retrieves a particular configuration based on its unique ID. 127 func Get(c *golangsdk.ServiceClient, appId, componentId string) (*Component, error) { 128 var r Component 129 _, err := c.Get(resourceURL(c, appId, componentId), &r, &golangsdk.RequestOpts{ 130 MoreHeaders: requestOpts.MoreHeaders, 131 }) 132 return &r, err 133 } 134 135 // ListOpts allows to filter list data using given parameters. 136 type ListOpts struct { 137 // Number of records to be queried. 138 // Value range: 0–100, or 1000. 139 // Default value: 1000, indicating that a maximum of 1000 records can be queried and all records are displayed on 140 // the same page. 141 Limit string `q:"limit"` 142 // The offset number. 143 Offset int `q:"offset"` 144 // Sorting field. By default, query results are sorted by creation time. 145 // The following enumerated values are supported: create_time, name, and update_time. 146 OrderBy string `q:"order_by"` 147 // Descending or ascending order. Default value: desc. 148 Order string `q:"order"` 149 } 150 151 // List is a method to query the list of the components using given opts. 152 func List(c *golangsdk.ServiceClient, appId string, opts ListOpts) ([]Component, error) { 153 url := rootURL(c, appId) 154 query, err := golangsdk.BuildQueryString(opts) 155 if err != nil { 156 return nil, err 157 } 158 url += query.String() 159 160 pages, err := pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page { 161 p := ComponentPage{pagination.OffsetPageBase{PageResult: r}} 162 return p 163 }).AllPages() 164 165 if err != nil { 166 return nil, err 167 } 168 return ExtractComponents(pages) 169 } 170 171 // UpdateOpts is the structure required by the Update method to update the component configuration. 172 type UpdateOpts struct { 173 // Application component name. 174 // The value can contain 2 to 64 characters, including letters, digits, hyphens (-), and underscores (_). 175 // It must start with a letter and end with a letter or digit. 176 Name string `json:"name,omitempty"` 177 // Description. 178 // The value can contain up to 128 characters. 179 Description *string `json:"description,omitempty"` 180 // Source of the code or software package. 181 Source *Source `json:"source,omitempty"` 182 // Component build. 183 Builder *Builder `json:"build,omitempty"` 184 } 185 186 // Update is a method to update the component configuration, such as name, description, builder and code source. 187 func Update(c *golangsdk.ServiceClient, appId, componentId string, opts UpdateOpts) (*Component, error) { 188 b, err := golangsdk.BuildRequestBody(opts, "") 189 if err != nil { 190 return nil, err 191 } 192 193 var r Component 194 _, err = c.Put(resourceURL(c, appId, componentId), b, &r, &golangsdk.RequestOpts{ 195 MoreHeaders: requestOpts.MoreHeaders, 196 }) 197 return &r, err 198 } 199 200 // Delete is a method to delete an existing component from a specified application. 201 func Delete(c *golangsdk.ServiceClient, appId, componentId string) error { 202 _, err := c.Delete(resourceURL(c, appId, componentId), &golangsdk.RequestOpts{ 203 MoreHeaders: requestOpts.MoreHeaders, 204 }) 205 return err 206 }