Support for attaching a Worker to a Domain
@@ -849,3 +849,47 @@ func getRouteEndpoint(route WorkerRoute) (string, error) {
return "routes", nil
}
+
+type WorkerDomain struct {
+ ZoneID string `json:"zone_id"`
+ Hostname string `json:"hostname"`
+ Service string `json:"service"`
+ Environment *string `json:"environment,omitempty"`
+}
+
+type WorkerDomainResult struct {
+ ID string `json:"id"`
+ ZoneID string `json:"zone_id"`
+ ZoneName string `json:"zone_name"`
+ Hostname string `json:"hostname"`
+ Service string `json:"service"`
+ Environment string `json:"environment"`
+}
+
+type WorkerDomainResponse struct {
+ Response
+ WorkerDomainResult `json:"result"`
+}
+
+// AttachWorkerToDomain attaches a worker to a zone and hostname
+//
+// API reference: https://api.cloudflare.com/#worker-domain-attach-to-domain
+func (api *API) AttachWorkerToDomain(ctx context.Context, zoneID, hostname, service string, environment *string) (WorkerDomainResponse, error) {
+ uri := fmt.Sprintf("/accounts/%s/workers/domains", api.AccountID)
+ body := WorkerDomain{
+ ZoneID: zoneID,
+ Hostname: hostname,
+ Service: service,
+ Environment: environment,
+ }
+ res, err := api.makeRequestContext(ctx, http.MethodPut, uri, body)
+ if err != nil {
+ return WorkerDomainResponse{}, err
+ }
+ var r WorkerDomainResponse
+ err = json.Unmarshal(res, &r)
+ if err != nil {
+ return WorkerDomainResponse{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
+ }
+ return r, nil
+}
