github.com/qiniu/go-sdk/v7@v7.27.0/sandbox/types_resource.go (about)

     1  package sandbox
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/qiniu/go-sdk/v7/sandbox/internal/apis"
     7  )
     8  
     9  // ---------------------------------------------------------------------------
    10  // SDK 自有类型 — 沙箱资源
    11  // ---------------------------------------------------------------------------
    12  
    13  // GitRepositoryType Git 仓库托管平台类型。
    14  type GitRepositoryType string
    15  
    16  // Git 仓库托管平台类型常量。后续若服务端扩展更多类型,将在此新增对应常量。
    17  const (
    18  	// GitRepositoryTypeGithub GitHub 仓库。
    19  	GitRepositoryTypeGithub GitRepositoryType = "github_repository"
    20  )
    21  
    22  // GitRepositoryResource Git 仓库资源,沙箱启动前由平台拉取并挂载快照到指定路径。
    23  // 同一沙箱内多个仓库资源当前必须共用同一 token。
    24  type GitRepositoryResource struct {
    25  	// Type 仓库托管平台类型(必填)。当前仅支持 GitRepositoryTypeGithub。
    26  	Type GitRepositoryType
    27  
    28  	// URL 仓库 URL(HTTPS 或 SSH 形式),如
    29  	// https://github.com/owner/repo.git 或 git@github.com:owner/repo.git。
    30  	URL string
    31  
    32  	// MountPath 仓库内容在沙箱内的绝对挂载路径。
    33  	MountPath string
    34  
    35  	// AuthorizationToken 用于克隆该仓库的访问 token。
    36  	// 同一沙箱内多个仓库资源当前必须共用同一 token。
    37  	AuthorizationToken *string
    38  }
    39  
    40  // KodoResource Kodo 存储桶资源,沙箱启动前由平台通过 NFS 代理挂载到指定路径。
    41  // 使用 Kodo 资源创建沙箱时,客户端必须可用 Qiniu AK/SK 凭证。
    42  type KodoResource struct {
    43  	// Bucket Kodo 存储桶名称(必填)。
    44  	Bucket string
    45  
    46  	// MountPath 存储桶内容在沙箱内的绝对挂载路径(必填)。
    47  	MountPath string
    48  
    49  	// Prefix 存储桶内可选的对象名前缀;不设置时挂载整个存储桶根目录。
    50  	Prefix *string
    51  
    52  	// ReadOnly 是否以只读方式挂载;当 AK/SK 缺少写权限时服务端也会自动只读。
    53  	ReadOnly *bool
    54  }
    55  
    56  // SandboxResourceSpec 沙箱资源规约(discriminated union),各字段互斥,只能设置一个。
    57  type SandboxResourceSpec struct {
    58  	// GitRepository GitHub 仓库资源。
    59  	GitRepository *GitRepositoryResource
    60  
    61  	// Kodo Kodo 存储桶资源。
    62  	Kodo *KodoResource
    63  }
    64  
    65  // ---------------------------------------------------------------------------
    66  // 转换函数 — SDK → apis
    67  // ---------------------------------------------------------------------------
    68  
    69  func sandboxResourceSpecToAPI(spec SandboxResourceSpec) (apis.SandboxResource, error) {
    70  	var r apis.SandboxResource
    71  	count := 0
    72  	if spec.GitRepository != nil {
    73  		count++
    74  	}
    75  	if spec.Kodo != nil {
    76  		count++
    77  	}
    78  	if count == 0 {
    79  		return r, fmt.Errorf("SandboxResourceSpec: exactly one resource type must be set (GitRepository or Kodo), got none")
    80  	}
    81  	if count > 1 {
    82  		return r, fmt.Errorf("SandboxResourceSpec: exactly one resource type must be set, but got %d", count)
    83  	}
    84  
    85  	switch {
    86  	case spec.GitRepository != nil:
    87  		if spec.GitRepository.Type == "" {
    88  			return r, fmt.Errorf("GitRepositoryResource.Type must be set (e.g. GitRepositoryTypeGithub)")
    89  		}
    90  		if spec.GitRepository.URL == "" {
    91  			return r, fmt.Errorf("GitRepositoryResource.URL must be set")
    92  		}
    93  		if spec.GitRepository.MountPath == "" {
    94  			return r, fmt.Errorf("GitRepositoryResource.MountPath must be set")
    95  		}
    96  		if spec.GitRepository.AuthorizationToken == nil || *spec.GitRepository.AuthorizationToken == "" {
    97  			return r, fmt.Errorf("GitRepositoryResource.AuthorizationToken must be set")
    98  		}
    99  		if err := r.FromGitRepositoryResource(apis.GitRepositoryResource{
   100  			URL:                spec.GitRepository.URL,
   101  			MountPath:          spec.GitRepository.MountPath,
   102  			AuthorizationToken: spec.GitRepository.AuthorizationToken,
   103  			Type:               apis.GitRepositoryResourceType(spec.GitRepository.Type),
   104  		}); err != nil {
   105  			return r, err
   106  		}
   107  	case spec.Kodo != nil:
   108  		if spec.Kodo.Bucket == "" {
   109  			return r, fmt.Errorf("KodoResource.Bucket must be set")
   110  		}
   111  		if spec.Kodo.MountPath == "" {
   112  			return r, fmt.Errorf("KodoResource.MountPath must be set")
   113  		}
   114  		if err := r.FromKodoResource(apis.KodoResource{
   115  			Bucket:    spec.Kodo.Bucket,
   116  			MountPath: spec.Kodo.MountPath,
   117  			Prefix:    spec.Kodo.Prefix,
   118  			ReadOnly:  spec.Kodo.ReadOnly,
   119  		}); err != nil {
   120  			return r, err
   121  		}
   122  	default:
   123  		return r, fmt.Errorf("SandboxResourceSpec: unsupported resource type")
   124  	}
   125  	return r, nil
   126  }