管理資源

資源 可配置或保留的實體或邏輯實例。資源的範例可包含儲存裝置、處理器、記憶體、資料庫、叢集及虛擬機器。 是您可以從目錄中建立的任何東西,由資源群組管理並包含在資源群組中。 您可以透過 IBM Cloud® 主控台的 資源清單 或使用命令列介面 (CLI) 來建立和管理資源。

使用 IBM Cloud Identity and Access Management(IAM)存取控制 來管理並屬於資源群組的服務有幾個好處。 由於資源群組不以位置為範圍,因此您可以在同一個資源群組中建立來自不同位置的應用程式和服務。 您也可以使用直到資源群組內個別實例的精細存取控制。

建立資源所需的存取權限

要使帳戶中的使用者能夠從目錄中建立資源並將其指定給資源群組,必須為他們指定兩個存取政策:

  • 對資源群組本身具有檢視者角色或更高角色的原則
  • 對帳戶中的服務具有編輯者角色或更高角色的原則

在主控台中建立資源

使用下列步驟在主控台中建立資源:

  1. 從您的儀表板中,按一下「資源」摘要小組件內的檢視資源
  2. 按一下建立資源。 從這裡,您會被引導到目錄。 您可以根據特定類別、供應商、定價方案、合規類型或發行類型搜尋產品或進行篩選。 資源的範例包括應用程式、服務實例、容器群集、儲存卷、虛擬伺服器和軟體。

建立資源後,該資源會顯示在資源清單頁面的資源清單中。

使用 CLI 建立資源

您可以使用 IBM Cloud® 指令行介面建立資源。 有關使用資源的詳細資訊,請參閱 使用資源和資源群組

  1. 登入,並選取帳戶。
    ibmcloud login
    
  2. 執行 ibmcloud resource service-instance-create 指令來建立組織。

在此命令中,NAME 是服務實例的名稱,SERVICE_NAME or SERVICE_ID 是服務的名稱或 ID,SERVICE_PLAN_NAME or SERVICE_PLAN_ID 是服務計劃的名稱或 ID,LOCATION 是建立服務實例的目標位置或環境。

ibmcloud resource service-instance-create NAME (SERVICE_NAME | SERVICE_ID) SERVICE_PLAN_NAME LOCATION [-d, --deployment DEPLOYMENT_NAME] [-p, --parameters @JSON_FILE | JSON_STRING ] [-g RESOURCE_GROUP] [--service-endpoints SERVICE_ENDPOINTS_TYPE] [--allow-cleanup] [--lock]

若要列出服務,請使用 ibmcloud catalog service-marketplace 命令。

例如,以下命令創建了一個服務實例,命名為 my-service-instance ,在位置 eu-gb 上使用服務 test-service 的服務計劃 test- service-plan

ibmcloud resource service-instance-create my-service-instance test-service test-service-plan eu-gb

使用 API 建立新的資源實體

您可以透過呼叫 Resource Controller API 程式化地建立新的資源實體,如以下範例請求所示。 有關 API 的詳細資訊,請參閱 Resource Controller API

curl -X POST \
  https://resource-controller.cloud.ibm.com/v2/resource_instances \
  -H 'Authorization: Bearer <>' \
  -H 'Content-Type: application/json' \
    -d '{
    "name": "my-instance",
    "target": "bluemix-global",
    "resource_group": "5g9f447903254bb58972a2f3f5a4c711",
    "resource_plan_id": "0be5ad401ae913d8ff665d92680664ed",
    "tags": [
      "my-tag"
    ]
  }'
CreateResourceInstanceOptions createResourceInstanceOptions = new CreateResourceInstanceOptions.Builder()
  .name(resourceInstanceName)
  .target(targetRegion)
  .resourceGroup(resourceGroup)
  .resourcePlanId(resourcePlanId)
  .build();
Response<ResourceInstance> response = service.createResourceInstance(createResourceInstanceOptions).execute();
ResourceInstance resourceInstance = response.getResult();
System.out.printf("createResourceInstance() response:\n%s\n", resourceInstance.toString());
const params = {
  name: resourceInstanceName,
  target: targetRegion,
  resourceGroup: resourceGroupGuid,
  resourcePlanId: resourcePlanId,
};
resourceControllerService.createResourceInstance(params)
  .then(res => {
    instanceGuid = res.result.guid;
    console.log('createResourceInstance() response:\n' + JSON.stringify(res.result, null, 2));
  })
  .catch(err => {
    console.warn(err)
   });
resource_instance = resource_controller_service.create_resource_instance(
    name=resource_instance_name,
    target=target_region,
    resource_group=resource_group,
    resource_plan_id=resource_plan_id
).get_result()
print('\ncreate_resource_instance() response:\n',
      json.dumps(resource_instance, indent=2))
createResourceInstanceOptions := resourceControllerService.NewCreateResourceInstanceOptions(
  resourceInstanceName,
  targetRegion,
  resourceGroup,
  resourcePlanID,
)
resourceInstance, response, err := resourceControllerService.CreateResourceInstance(createResourceInstanceOptions)
if err != nil {
  panic(err)
}
b, _ := json.MarshalIndent(resourceInstance, "", "  ")
fmt.Printf("\nCreateResourceInstance() response:\n%s\n", string(b))

使用 API 安裝軟體

您只能透過主控台或 CLI 安裝軟體。 若要查看步驟,請切換至 UI 或 CLI 指示。

使用 Terraform 建立新的資源實體

在使用 Terraform 創建新的資源實例之前,請確認您已完成下列事項:

  • 安裝 Terraform CLI 並為 Terraform 配置 IBM Cloud Provider 外掛程式。 如需詳細資訊,請參閱 IBM Cloud® 上的 Terraform 入門 教學。 外掛程式抽象出 IBM Cloud API,用來完成這項任務。

  • 建立名為 main.tf 的 Terraform 配置檔案。 在此檔案中,您使用 HashiCorp Configuration Language 定義資源。 如需詳細資訊,請參閱 Terraform 文件

您可以使用 Terraform 建立新的資源實體。

  1. main.tf 檔案中建立一個參數。 以下範例使用 ibm_resource_instance 資源建立新的資源實體,其中 name 是唯一的描述性名稱,用以識別資源實體。

    data "ibm_resource_group" "group" {
    name = "test"
    }
    resource "ibm_resource_instance" "resource_instance" {
     name              = "test"
     service           = "cloud-object-storage"
     plan              = "lite"
     location          = "global"
     resource_group_id = data.ibm_resource_group.group.id
     tags              = ["tag1", "tag2"]
     //User can increase timeouts
     timeouts {
     create = "15m"
     update = "15m"
     delete = "15m"
     }
    }
    

    您可以指定與資源實體相關的 tags。 如需詳細資訊,請參閱 Terraform 資源管理頁面中的參數參考細節。

  2. 完成建立組態檔案後,初始化 Terraform CLI。 如需詳細資訊,請參閱 初始化工作目錄

    terraform init
    
  3. main.tf 檔案提供資源。 如需詳細資訊,請參閱 使用 Terraform 佈建基礎結構

    1. 執行 terraform plan 以產生 Terraform 執行計畫,預覽建議的動作。
      terraform plan
      
    2. 執行 terraform apply 以建立計劃中定義的資源。
      terraform apply
      

搜尋資源

您可以從 IBM Cloud® 主控台任意處搜尋資源。 在主控台功能表列的搜尋欄位中,輸入資源或標籤。 您也可以使用 IBM Cloud 指令行介面 (CLI),在資源之間搜尋。 CLI 會跨各個位置和資料中心搜尋分散式應用程式和服務實例。 全球搜尋與標籤 - 搜尋 API 也支援搜尋資源。

精簡搜尋結果

檢視所有型錄結果
使用此選項來檢視已過濾的型錄搜尋。 前五個結果會依名稱顯示。 如果您想要使用更詳細的型錄項目搜尋準則(例如搜尋說明內容),可以按一下此鏈結並取得已過濾的型錄結果視圖。 此選項可協助您尋找想要更快建立的供應項目。 如果符合型錄結果,就會出現這個欄位。 如果搜尋查詢的開頭為 tag:,則不會出現這個欄位。
搜尋支援案例
使用此選項可依整個平台中的已開立支援案例進行過濾,包括基礎架構資源。 如果您搜尋任何東西(包括依 tag: 進行搜尋),這個選項就會出現在搜尋尾端。
在 IBM Cloud 文件中搜尋
使用此選項,可以取得已過濾的文件搜尋。 這會顯示在任何搜尋的最後,包括您以 tag: 搜尋。

按下正斜線鍵 (/),將游標導覽至搜尋欄位。

使用 CLI 進行搜尋

您也可以使用 Lucene 查詢語法,透過 IBM Cloud CLI 以單一指令搜尋所有資源。 您可以查詢和擷取的最常用資源屬性有

name
資源的使用者定義名稱。
region
建立資源的地理位置。 例如,允許的值為 us-south, us-east, au-syd, eu-gb, eu-de,和 jp-tok
service_name
資源 CRN 的 service-name 區段中出現的服務名稱。 您可以從 ibmcloud catalog service-marketplace 輸出的 Name 列取得允許的值。
resource_group_id
資源的資源群組 ID。
type
資源 CRN resource-type 段中出現的資源類型(如果有)。 值取決於擁有的服務。 範例值:k8-cluster 代表 containers-kubernetes 服務; k8-locationk8-connector 代表 satellite 服務; resource-group 代表 resource-controller 服務; vpc, volume, vpn, load-balancer, security-group,代表 is 服務; workspace 代表 schematics 服務。
creation_date
資源的建立日期。
modification_date
資源的前次修改日期。
tags
附加到資源的標籤。
access_tags
附加到資源的存取管理標籤。
service_tags
附加到資源的服務標籤。
tagReferences.tag.name
已附加至標準基礎架構資源的標籤。 需要您指定 -p classic-infrastructure 參數。
_objectType:
標準基礎架構資源的物件類型。 允許的值有 SoftLayer_Virtual_DedicatedHost, SoftLayer_Hardware, SoftLayer_Network_Application_Delivery_Controller, SoftLayer_Network_Subnet_IpAddress, SoftLayer_Network_Vlan, SoftLayer_Network_Vlan_Firewall, SoftLayer_Virtual_Guest。需要您指定 -p classic-infrastructure 參數。

tagReferences.tag.name_objectType 僅在使用 -p classic-infrastructure 標誌時有效。

-p classic-infrastructure 已不再適用於 _objectType SoftLayer_Virtual_DedicatedHost, SoftLayer_Network_Vlan_Firewall, SoftLayer_Virtual_GuestSoftLayer_Hardware (僅適用於經典基礎架構裸機伺服器),因為現在這些資源和其他非經典基礎架構資源一樣,都是可搜尋的。

搜尋經典基礎結構資源

若要搜尋經典基礎結構資源,字串必須包含在雙引號中,才能傳回完全符合的查詢字串。 此外,如果您輸入的搜尋字詞包含連字號 (-),而您沒有在字串周圍加上雙引號,搜尋將不會傳回完全符合的結果。 字串中的連字線用來將術語分成多個字串。

搜尋範例

下列範例可協助您搜尋帳戶資源。 未指定 -p classic-infrastucture 參數時,搜尋範圍涵蓋所有資源,但不包括 _objectType SoftLayer_Network_Application_Delivery_ControllerSoftLayer_Network_Subnet_IpAddressSoftLayer_Hardware 的經典基礎結構資源 (不包括裸機伺服器)。

  • 若要搜尋名為 ABC 的所有資源,請輸入下列指令:

    ibmcloud resource search ‘name:ABC’
    
  • 若要搜尋 Message Hub 的所有服務實例,請輸入下列指令:

    ibmcloud resource search 'service_name:messagehub'
    
  • 若要搜尋在 2020 年 5 月 16 日至 2020 年 5 月 20 日期間建立的非經典基礎結構資源,請輸入下列指令:

    ibmcloud resource search 'creation_date:["2020-05-16T00:00:00Z" TO "2020-05-20T00:00:00Z"]'
    
  • 若要搜尋不是標準基礎架構的資源,其名稱開頭為 "my" 且依類型排序,請輸入下列指令:

    ibmcloud resource search 'name:my*' -s type
    
  • 若要搜尋並非經典基礎架構,且已加上 my-tag 標籤的資源,請輸入下列指令:

    ibmcloud resource search 'tags:my-tag'
    
  • 若要搜尋完全合格網域名稱為 MyVM 的所有 classic infrastructure 虛擬伺服器,請輸入下列指令:

    ibmcloud resource search “doc.fullyQualifiedDomainName:MyVM AND service_name:virtual-server"
    
  • 若要搜尋已標記為 my-tag 的所有經典基礎結構資源,請輸入下列指令:

    ibmcloud resource search 'tagReferences.tag.name:my-tag' -p classic-infrastructure
    
  • 若要搜尋所有類型為 SoftLayer_Network_Vlan 的經典基礎結構,請輸入下列指令:

    ibmcloud resource search '_objectType:SoftLayer_Network_Vlan' -p classic-infrastructure
    

使用 API 搜尋

若要搜尋資源,請呼叫 The Global Search and Tagging - Search API。 以下範例會搜尋所有附有標籤「project:myproject」的資源。

使用 SearchOptions.Builder 建立 SearchOptions 物件,其中包含 search 方法的參數值。

實體化 SearchOptions 結構,並設定欄位以提供 Search 方法的參數值。

curl -X POST -H "Authorization: {iam_token}" -H "Accept: application/json" -H "Content-Type: application/json" -d '{"query": "tags:project\\:myproject OR access_tags:project\\:myproject", "fields": ["*"]}' "api.global-search-tagging.cloud.ibm.com/v3/resources/search"
SearchOptions searchOptions = new SearchOptions.Builder()
  .query("GST-sdk-*")
  .fields(new java.util.ArrayList<String>(java.util.Arrays.asList("*")))
  .searchCursor(searchCursor)
  .build();

Response<ScanResult> response = service.search(searchOptions).execute();
ScanResult scanResult = response.getResult();

System.out.println(scanResult);
const params = {
  query: 'GST-sdk-*',
  fields: ['*'],
  searchCursor: searchCursor,
};

globalSearchService.search(params)
  .then(res => {
    console.log(JSON.stringify(res.result, null, 2));
  })
  .catch(err => {
    console.warn(err)
  });
response = global_search_service.search(query='GST-sdk-*',
                    fields=['*'])
scan_result = response.get_result()

print(json.dumps(scan_result, indent=2))
searchOptions := globalSearchService.NewSearchOptions()
searchOptions.SetLimit(10)
searchOptions.SetQuery("GST-sdk-*")
searchOptions.SetFields([]string{"*"})

scanResult, response, err := globalSearchService.Search(searchOptions)
if err != nil {
  panic(err)
}
b, _ := json.MarshalIndent(scanResult, "", "  ")
fmt.Println(string(b))

刪除資源

當您不再需要帳戶中的資源,或帳戶中的使用者建立了您不希望他們使用的資源時,您可以從帳戶中刪除該實例。

在主控台中刪除資源

您可以使用下列步驟在主控台中刪除資源:

  1. 從儀表板,按一下資源摘要 Widget 中的檢視資源
  2. 展開區段來尋找您要刪除的服務實例。
  3. 按一下該行的動作圖示動作圖示 > 刪除

使用 CLI 刪除資源

您可以使用 IBM Cloud® 指令行介面刪除資源。 如需管理 IBM Cloud 資源的詳細資訊,請參閱 使用資源和資源群組

  1. 登入,並選取帳戶。

    ibmcloud login
    
  2. 透過執行 ibmcloud resource service-instance-delete 命令,其中 NAME 是服務實例的名稱,與 ID 排他,而 ID 是服務實例的 ID,與 NAME 排他。

    ibmcloud resource service-instance-delete (NAME|ID) [-f, --force] [--recursive]
    

    例如,以下命令會刪除一個名為 my-service-instance 的資源服務實體:

    ibmcloud resource service-instance-delete my-service-instance
    

使用 API 刪除資源實體

您可以透過呼叫 Resource Controller API 程式化刪除資源實體,如以下範例請求所示。 有關 API 的詳細資訊,請參閱 Resource Controller API

curl -X DELETE \
https://resource-controller.cloud.ibm.com/v2/resource_instances/8d7af921-b136-4078-9666-081bd8470d94 \
  -H 'Authorization: Bearer <>'
DeleteResourceInstanceOptions deleteResourceInstanceOptions = new DeleteResourceInstanceOptions.Builder()
  .id(instanceGuid)
  .recursive(false)
  .build();

Response<Void> response = service.deleteResourceInstance(deleteResourceInstanceOptions).execute();

System.out.printf("deleteResourceInstance() response status code: %d\n", response.getStatusCode());
const params = {
  id: instanceGuid,
  recursive: false,
};

resourceControllerService.deleteResourceInstance(params)
  .then(res => {
    console.log('deleteResourceInstance() response status code: ' + res.status);
  })
  .catch(err => {
    console.warn(err)
   });
response = resource_controller_service.delete_resource_instance(
    id=instance_guid,
    recursive=False
)

print('\ndelete_resource_instance() response status code: ',
      response.get_status_code())
deleteResourceInstanceOptions := resourceControllerService.NewDeleteResourceInstanceOptions(
  instanceGUID,
)
deleteResourceInstanceOptions.SetRecursive(false)

response, err := resourceControllerService.DeleteResourceInstance(deleteResourceInstanceOptions)
if err != nil {
  panic(err)
}
fmt.Printf("\nDeleteResourceInstance() response status code: %d\n", response.StatusCode)

使用 Terraform 刪除資源實體

使用以下步驟透過 Terraform 刪除資源實體。

  1. 您可以從 terraform 檔案中移除下列程式碼區塊,以刪除資源實例。 您必須使用 terraform 檔案建立 resource_instance

    data "ibm_resource_group" "group" {
    name = "test"
    }
    resource "ibm_resource_instance" "resource_instance" {
     name              = "test"
     service           = "cloud-object-storage"
     plan              = "lite"
     location          = "global"
     resource_group_id = data.ibm_resource_group.group.id
     tags              = ["tag1", "tag2"]
     //User can increase timeouts
     timeouts {
     create = "15m"
     update = "15m"
     delete = "15m"
     }
    }
    

    如需詳細資訊,請參閱 Terraform 資源管理頁面中的參數參考細節。

  2. 完成建立組態檔案後,初始化 Terraform CLI。 如需詳細資訊,請參閱 初始化工作目錄

    terraform init
    
  3. main.tf 檔案提供資源。 如需詳細資訊,請參閱 使用 Terraform 佈建基礎結構

    1. 執行 terraform plan 以產生 Terraform 執行計畫,預覽建議的動作。

      terraform plan
      
    2. 執行 terraform apply 以建立計劃中定義的資源。

      terraform apply
      

您也可以執行下列 terraform destroy 指令刪除資源實體:

terraform destroy -target RESOURCE_TYPE.NAME -target RESOURCE_TYPE2.NAME