json.Marshal(): json: 为 msgraph.Application 类型调用 MarshalJSON 时出错

Posted

技术标签:

【中文标题】json.Marshal(): json: 为 msgraph.Application 类型调用 MarshalJSON 时出错【英文标题】:json.Marshal(): json: error calling MarshalJSON for type msgraph.Application 【发布时间】:2021-11-26 05:47:40 【问题描述】:

必须进行哪些特定的语法或配置更改才能解决以下错误,即 terraform 无法创建 azuread_application 的实例?

代码:

terraform apply运行时触发错误的terraform代码如下:

variable "tenantId"  
variable "clientId"  
variable "clientSecret"  
variable "instanceName"  

terraform 
  required_providers 
    azuread = 
      source  = "hashicorp/azuread"
      version = "2.5.0"
    
  


provider "azuread" 
  tenant_id       = var.tenantId
  client_id       = var.clientId
  client_secret   = var.clientSecret


resource "azuread_application" "appRegistration" 
  display_name = var.instanceName
  app_role 
    allowed_member_types = ["User", "Application"]
    description          = "Admins can manage roles and perform all task actions"
    display_name         = "Admin"
    enabled              = true
    id                   = "1b19509b-32b1-4e9f-b71d-4992aa991967"
    value                = "admin"
  

错误:

使用terraform apply 运行上述代码产生的错误和日志输出是:

2021/10/05 17:47:18 [DEBUG] module.ad-admin.azuread_application.appRegistration:
 apply errored, but we're indicating that via the Error pointer rather than returning it:
 Could not create application: json.Marshal():
 json: error calling MarshalJSON for type msgraph.Application:
 json: error calling MarshalJSON for type *msgraph.Owners: marshaling Owners: encountered DirectoryObject with nil ODataId

2021/10/05 17:47:18 [TRACE] EvalMaybeTainted: module.ad-admin.azuread_application.appRegistration encountered an error during creation, so it is now marked as tainted
2021/10/05 17:47:18 [TRACE] EvalWriteState: removing state object for module.ad-admin.azuread_application.appRegistration
2021/10/05 17:47:18 [TRACE] EvalApplyProvisioners: azuread_application.appRegistration has no state, so skipping provisioners
2021/10/05 17:47:18 [TRACE] EvalMaybeTainted: module.ad-admin.azuread_application.appRegistration encountered an error during creation, so it is now marked as tainted
2021/10/05 17:47:18 [TRACE] EvalWriteState: removing state object for module.ad-admin.azuread_application.appRegistration
2021/10/05 17:47:18 [TRACE] vertex "module.ad-admin.azuread_application.appRegistration": visit complete

2021/10/05 17:47:18 [TRACE] dag/walk: upstream of "module.ad-admin.output.application_id (expand)" errored, so skipping
2021/10/05 17:47:18 [TRACE] dag/walk: upstream of "module.ad-admin.azuread_service_principal.appRegistrationSP" errored, so skipping
2021/10/05 17:47:18 [TRACE] dag/walk: upstream of "output.application_id" errored, so skipping
2021/10/05 17:47:18 [TRACE] dag/walk: upstream of "module.ad-admin.output.appId (expand)" errored, so skipping
2021/10/05 17:47:18 [TRACE] dag/walk: upstream of "module.ad-admin.azuread_service_principal_password.appRegistrationSP_pwd" errored, so skipping
2021/10/05 17:47:18 [TRACE] dag/walk: upstream of "output.appId" errored, so skipping
2021/10/05 17:47:18 [TRACE] dag/walk: upstream of "module.ad-admin.azurerm_role_assignment.appRegistrationSP_role_assignment_vault" errored, so skipping
2021/10/05 17:47:18 [TRACE] dag/walk: upstream of "module.ad-admin.azurerm_role_assignment.appRegistrationSP_role_assignment" errored, so skipping
2021/10/05 17:47:18 [TRACE] dag/walk: upstream of "module.ad-admin.provider[\"registry.terraform.io/hashicorp/azuread\"] (close)" errored, so skipping
2021/10/05 17:47:18 [TRACE] dag/walk: upstream of "module.ad-admin.provider[\"registry.terraform.io/hashicorp/azurerm\"] (close)" errored, so skipping
2021/10/05 17:47:18 [TRACE] dag/walk: upstream of "module.ad-admin (close)" errored, so skipping
2021/10/05 17:47:18 [TRACE] dag/walk: upstream of "meta.count-boundary (EachMode fixup)" errored, so skipping
2021/10/05 17:47:18 [TRACE] dag/walk: upstream of "root" errored, so skipping
2021/10/05 17:47:18 [TRACE] statemgr.Filesystem: creating backup snapshot at terraform.tfstate.backup
2021/10/05 17:47:18 [TRACE] statemgr.Filesystem: state has changed since last snapshot, so incrementing serial to 391
2021/10/05 17:47:18 [TRACE] statemgr.Filesystem: writing snapshot at terraform.tfstate
2021/10/05 17:47:18 [TRACE] statemgr.Filesystem: removing lock metadata file .terraform.tfstate.lock.info

Error: Could not create application

  on ..\..\..\..\modules\ad-admin\active-directory.tf line 69, in resource "azuread_application" "appRegistration":
  69: resource "azuread_application" "appRegistration" 

json.Marshal(): json: error calling MarshalJSON for type msgraph.Application:
json: error calling MarshalJSON for type *msgraph.Owners: marshaling Owners:
2021/10/05 17:47:18 [TRACE] statemgr.Filesystem: unlocked by closing terraform.tfstate
encountered DirectoryObject with nil ODataId

terraform -version 给出:

地形 v1.0.8 在 windows_amd64 上

【问题讨论】:

您使用的是最新版本的 TF 和 azure 提供程序吗? 哦,我明白了。这是相当老的供应商。你检查过最新的吗? 进展如何?仍然不清楚发生了什么? @Marcin 感谢您的跟进。我将解决方案作为对您答案的编辑并将其标记为已接受以帮助他人。 【参考方案1】:

这是一个错误,报告为 GitHub 问题:

Error: ODataId was nil when creating an azuread_group resource #588

解决OP中问题的方法是从上面的OP中的代码将required_providers块中的2.5.0版本升级为2.6.0,如下所示:

terraform 
  required_providers 
    azuread = 
      source  = "hashicorp/azuread"
      version = "2.6.0"
    
  

【讨论】:

【参考方案2】:

修复已发布,它对我有用。

Workaround for corrupted or missing @odata.id for directory objects

【讨论】:

以上是关于json.Marshal(): json: 为 msgraph.Application 类型调用 MarshalJSON 时出错的主要内容,如果未能解决你的问题,请参考以下文章

http.newRequest 的 json.Marshal 正文如何

为啥我的 json.Marshal() 啥也没返回? [复制]

golangGo中json.Marshal函数

golang里的json marshal && unmarshal

踩了大坑 : go json.Marshal时,结构体字段需要大写

Go读写文件