云审计服务 CTS-创建追踪器:Go

时间:2023-12-22 17:31:29

Go

  • 管理类追踪器创建样例。
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    package main
    
    import (
    	"fmt"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
        cts "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cts/v3"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cts/v3/model"
        region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cts/v3/region"
    )
    
    func main() {
        // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
        // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
        ak := os.Getenv("CLOUD_SDK_AK")
        sk := os.Getenv("CLOUD_SDK_SK")
    
        auth := basic.NewCredentialsBuilder().
            WithAk(ak).
            WithSk(sk).
            Build()
    
        client := cts.NewCtsClient(
            cts.CtsClientBuilder().
                WithRegion(region.ValueOf("cn-north-4")).
                WithCredential(auth).
                Build())
    
        request := &model.CreateTrackerRequest{}
    	bucketNameObsInfo:= "test-data-tracker"
    	filePrefixNameObsInfo:= "11"
    	isObsCreatedObsInfo:= false
    	obsInfobody := &model.TrackerObsInfo{
    		BucketName: &bucketNameObsInfo,
    		FilePrefixName: &filePrefixNameObsInfo,
    		IsObsCreated: &isObsCreatedObsInfo,
    	}
    	isSupportValidateCreateTrackerRequestBody:= true
    	kmsIdCreateTrackerRequestBody:= "13a4207c-7abe-4b68-8510-16b84c3b5504"
    	isSupportTraceFilesEncryptionCreateTrackerRequestBody:= true
    	isLtsEnabledCreateTrackerRequestBody:= true
    	request.Body = &model.CreateTrackerRequestBody{
    		IsSupportValidate: &isSupportValidateCreateTrackerRequestBody,
    		KmsId: &kmsIdCreateTrackerRequestBody,
    		IsSupportTraceFilesEncryption: &isSupportTraceFilesEncryptionCreateTrackerRequestBody,
    		ObsInfo: obsInfobody,
    		IsLtsEnabled: &isLtsEnabledCreateTrackerRequestBody,
    		TrackerName: "system",
    		TrackerType: model.GetCreateTrackerRequestBodyTrackerTypeEnum().SYSTEM,
    	}
    	response, err := client.CreateTracker(request)
    	if err == nil {
            fmt.Printf("%+v\n", response)
        } else {
            fmt.Println(err)
        }
    }
    
  • 数据类追踪器创建样例。
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    package main
    
    import (
    	"fmt"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
        cts "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cts/v3"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cts/v3/model"
        region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cts/v3/region"
    )
    
    func main() {
        // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
        // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
        ak := os.Getenv("CLOUD_SDK_AK")
        sk := os.Getenv("CLOUD_SDK_SK")
    
        auth := basic.NewCredentialsBuilder().
            WithAk(ak).
            WithSk(sk).
            Build()
    
        client := cts.NewCtsClient(
            cts.CtsClientBuilder().
                WithRegion(region.ValueOf("cn-north-4")).
                WithCredential(auth).
                Build())
    
        request := &model.CreateTrackerRequest{}
    	var listDataEventDataBucket = []model.DataBucketDataEvent{
            model.GetDataBucketDataEventEnum().READ,
    	    model.GetDataBucketDataEventEnum().WRITE,
        }
    	dataBucketNameDataBucket:= "cstest0423"
    	dataBucketbody := &model.DataBucket{
    		DataBucketName: &dataBucketNameDataBucket,
    		DataEvent: &listDataEventDataBucket,
    	}
    	bucketNameObsInfo:= "saveTraceBucket"
    	filePrefixNameObsInfo:= "11"
    	isObsCreatedObsInfo:= false
    	bucketLifecycleObsInfo:= int32(30)
    	obsInfobody := &model.TrackerObsInfo{
    		BucketName: &bucketNameObsInfo,
    		FilePrefixName: &filePrefixNameObsInfo,
    		IsObsCreated: &isObsCreatedObsInfo,
    		BucketLifecycle: &bucketLifecycleObsInfo,
    	}
    	isLtsEnabledCreateTrackerRequestBody:= true
    	request.Body = &model.CreateTrackerRequestBody{
    		DataBucket: dataBucketbody,
    		ObsInfo: obsInfobody,
    		IsLtsEnabled: &isLtsEnabledCreateTrackerRequestBody,
    		TrackerName: "data-tracker-name",
    		TrackerType: model.GetCreateTrackerRequestBodyTrackerTypeEnum().DATA,
    	}
    	response, err := client.CreateTracker(request)
    	if err == nil {
            fmt.Printf("%+v\n", response)
        } else {
            fmt.Println(err)
        }
    }
    
support.huaweicloud.com/api-cts/cts_api_0321.html